forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexceptions.jl
260 lines (253 loc) · 6.47 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
using Test
@testset "Basic exception stack handling" begin
# Exiting the catch block normally pops the exception
try
error("A")
catch
@test length(catch_stack()) == 1
end
@test length(catch_stack()) == 0
# Exiting via a finally block does not pop the exception
try
try
error("A")
finally
@test length(catch_stack()) == 1
end
catch
@test length(catch_stack()) == 1
end
# The combined try-catch-finally form obeys the same rules as above
try
error("A")
catch
@test length(catch_stack()) == 1
finally
@test length(catch_stack()) == 0
end
@test length(catch_stack()) == 0
# Errors are pushed onto the stack according to catch block nesting
try
error("RootCause")
catch
@test length(catch_stack()) == 1
try
error("B")
catch
stack = catch_stack()
@test length(stack) == 2
@test stack[1][1].msg == "RootCause"
@test stack[2][1].msg == "B"
end
# Stack pops correctly
stack = catch_stack()
@test length(stack) == 1
@test stack[1][1].msg == "RootCause"
end
end
@testset "Exception stack lowering special cases" begin
# try block in value position
val = try
error("A")
catch
@test length(catch_stack()) == 1
1
end
@test val == 1
function test_exc_stack_tailpos()
# try block in tail position
try
error("A")
catch
length(catch_stack())
end
end
@test test_exc_stack_tailpos() == 1
@test length(catch_stack()) == 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(catch_stack()) == 1
return
end
end
test_exc_stack_catch_return()
for i=1:1
try
error("A")
catch
@test length(catch_stack()) == 1
break
end
end
for i=1:1
try
error("A")
catch
@test length(catch_stack()) == 1
continue
end
end
try
error("A")
catch
@test length(catch_stack()) == 1
@goto outofcatch
end
@label outofcatch
@test length(catch_stack()) == 0
# Exiting from a try block in various ways should not affect the exception
# stack state.
try
error("ExceptionInOuterTry")
catch
@test length(catch_stack()) == 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(catch_stack()) == 1
@test catch_stack()[1][1] == ErrorException("ExceptionInOuterTry")
end
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 catch_stack()[1][1] == ErrorException("RootCause")
length(catch_stack())
end == 100
@test length(catch_stack()) == 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(catch_stack()) == 1
@test catch_stack()[1][1] == ErrorException("A")
end
@test length(catch_stack()) == 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(catch_stack()) == 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(catch_stack()) == 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 catch_stack(t, include_bt=false) == [ErrorException("A"), ErrorException("B")]
# 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 == ErrorException("expected")
@test length(catch_stack(t)) == 1
@test length(catch_stack(t)[1][2]) > 0 # backtrace is nonempty
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