forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exceptions.jl
402 lines (384 loc) · 10.8 KB
/
exceptions.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# This file is a part of Julia. License is MIT: https://julialang.org/license
using Test
@testset "Basic exception stack handling" begin
# Exiting the catch block normally pops the exception
try
error("A")
catch
@test length(current_exceptions()) == 1
end
@test length(current_exceptions()) == 0
# Exiting via a finally block does not pop the exception
try
try
error("A")
finally
@test length(current_exceptions()) == 1
end
catch
@test length(current_exceptions()) == 1
end
# The combined try-catch-finally form obeys the same rules as above
try
error("A")
catch
@test length(current_exceptions()) == 1
finally
@test length(current_exceptions()) == 0
end
@test length(current_exceptions()) == 0
# Errors are pushed onto the stack according to catch block nesting
try
error("RootCause")
catch
@test length(current_exceptions()) == 1
try
error("B")
catch
stack = current_exceptions()
@test length(stack) == 2
@test stack[1].exception.msg == "RootCause"
@test stack[2].exception.msg == "B"
end
# Stack pops correctly
stack = current_exceptions()
@test length(stack) == 1
@test stack[1].exception.msg == "RootCause"
end
end
@testset "Exception stack lowering special cases" begin
# try block in value position
val = try
error("A")
catch
@test length(current_exceptions()) == 1
1
end
@test val == 1
function test_exc_stack_tailpos()
# try block in tail position
try
error("A")
catch
length(current_exceptions())
end
end
@test test_exc_stack_tailpos() == 1
@test length(current_exceptions()) == 0
end
@testset "Exception stacks - early exit from try or catch" begin
# Exiting a catch block early with normal control flow — break, continue,
# return, goto — will result in popping of the exception stack.
function test_exc_stack_catch_return()
try
error("A")
catch
@test length(current_exceptions()) == 1
return
end
end
test_exc_stack_catch_return()
for i=1:1
try
error("A")
catch
@test length(current_exceptions()) == 1
break
end
end
# Also test try-break-finally forms here. See #31766
for i=1:1
try
error("A")
catch
@test length(current_exceptions()) == 1
break
finally
@test length(current_exceptions()) == 0
end
end
@test length(current_exceptions()) == 0
for i=1:1
try
error("A")
catch
@test length(current_exceptions()) == 1
continue
end
end
for i=1:1
try
error("A")
catch
@test length(current_exceptions()) == 1
continue
finally
@test length(current_exceptions()) == 0
end
end
@test length(current_exceptions()) == 0
try
error("A")
catch
@test length(current_exceptions()) == 1
@goto outofcatch
end
@label outofcatch
try
error("A")
catch
@test length(current_exceptions()) == 1
@goto outofcatch2
finally
@test length(current_exceptions()) == 0
end
@label outofcatch2
@test length(current_exceptions()) == 0
# Exiting from a try block in various ways should not affect the exception
# stack state.
try
error("ExceptionInOuterTry")
catch
@test length(current_exceptions()) == 1
function test_exc_stack_try_return()
try
return
catch
end
end
test_exc_stack_try_return()
for i=1:1
try
break
catch
end
end
for i=1:1
try
continue
catch
end
end
try
@goto outoftry
catch
end
@label outoftry
@test length(current_exceptions()) == 1
@test current_exceptions()[1].exception == ErrorException("ExceptionInOuterTry")
end
end
@testset "Finally handling with exception stacks" begin
# The lowering of finally is quite subtle when combined with break or
# return because each finally block may be entered via multiple code paths
# (eg, different occurrences of return), and these code paths must diverge
# again once the finally block has completed. To complicate matters
# further, the return code path must thread through every nested finally
# block before actually returning, all the while preserving the information
# about which variable to return.
# Issue #34579
(()-> begin
try
throw("err")
catch
# Explicit return => exception should be popped before finally block
return
finally
@test length(Base.current_exceptions()) == 0
end
end)()
@test length(Base.current_exceptions()) == 0
while true
try
error("err1")
catch
try
# Break target is outside catch block, but finally is inside =>
# exception should not be popped inside finally block
break
finally
@test length(Base.current_exceptions()) == 1
end
end
end
@test length(Base.current_exceptions()) == 0
# Nested finally handling with `return`: each finally block should observe
# only the active exceptions as according to its nesting depth.
(() -> begin
try
try
error("err1")
catch
try
try
error("err2")
catch
# This return needs to thread control flow through
# multiple finally blocks in the linearized IR.
return
end
finally
# At this point err2 is dealt with
@test length(Base.current_exceptions()) == 1
@test Base.current_exceptions()[1].exception == ErrorException("err1")
end
end
finally
# At this point err1 is dealt with
@test length(Base.current_exceptions()) == 0
end
end)()
@test length(Base.current_exceptions()) == 0
end
@testset "Deep exception stacks" begin
# Generate deep exception stack with recursive handlers.
#
# (Note that if you let this overflow the program stack (not the exception
# stack) julia will crash. See #28577.)
function test_exc_stack_deep(n)
n != 1 || error("RootCause")
try
test_exc_stack_deep(n-1)
catch
error("n==$n")
end
end
@test try
test_exc_stack_deep(100)
catch
@test current_exceptions()[1].exception == ErrorException("RootCause")
length(current_exceptions())
end == 100
@test length(current_exceptions()) == 0
end
@testset "Exception stacks and Tasks" begin
# Task switching should not affect exception state. See #12485.
try
error("A")
catch
t = @task try
error("B")
catch exc
exc
end
yield(t)
@test t.state == :done
@test t.result == ErrorException("B")
# Task exception state is preserved around task switches
@test length(current_exceptions()) == 1
@test current_exceptions()[1].exception == ErrorException("A")
end
@test length(current_exceptions()) == 0
# test rethrow() rethrows correct state
bt = []
try
try
error("A")
catch
bt = catch_backtrace()
t = @task try
error("B")
catch exc
exc
end
yield(t)
@test t.state == :done
@test t.result == ErrorException("B")
@test bt == catch_backtrace()
rethrow()
end
catch exc
@test exc == ErrorException("A")
@test bt == catch_backtrace()
end
@test length(current_exceptions()) == 0
# test rethrow with argument
bt = []
try
try
error("A")
catch
t = @task try
error("B")
catch exc
exc
end
yield(t)
@test t.state == :done
@test t.result == ErrorException("B")
bt = catch_backtrace()
rethrow(ErrorException("C"))
end
catch exc
@test exc == ErrorException("C")
@test bt == catch_backtrace()
end
@test length(current_exceptions()) == 0
# Exception stacks on other tasks
t = @task try
error("A")
catch
error("B")
end
yield(t)
@test t.state == :failed
@test t.result == ErrorException("B")
@test current_exceptions(t, backtrace=false) == [
(exception=ErrorException("A"),backtrace=nothing),
(exception=ErrorException("B"),backtrace=nothing)
]
# Exception stacks for tasks which never get the chance to start
t = @task nothing
@test (try
@async Base.throwto(t, ErrorException("expected"))
wait(t)
catch e
e
end).task.exception == ErrorException("expected")
@test length(current_exceptions(t)) == 1
@test length(current_exceptions(t)[1].backtrace) > 0 # backtrace is nonempty
# Exception stacks should not be accessed on concurrently running tasks
t = @task ()->nothing
@test_throws ErrorException("Inspecting the exception stack of a task which might "*
"be running concurrently isn't allowed.") current_exceptions(t)
end
@testset "rethrow" begin
@test try
rethrow()
catch exc
exc
end == ErrorException("rethrow() not allowed outside a catch block")
@test try
rethrow(ErrorException("A"))
catch exc
exc
end == ErrorException("rethrow(exc) not allowed outside a catch block")
end
# issue #36527
function f36527()
caught = false
🏡 = Core.eval(Main, :(module asdf36527 end))
try
Core.eval(🏡, :(include_string($🏡, "@assert z36527 == 10")))
catch ex
GC.gc()
catch_backtrace()
caught = true
end
return caught
end
@test f36527()
# accessing an undefined var in tail position in a catch block
function undef_var_in_catch()
try
error("first error")
catch
__probably_n0t_defined__
end
end
@test length(try
undef_var_in_catch()
[]
catch
current_exceptions()
end) == 2