-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathexception.py
369 lines (304 loc) · 6.98 KB
/
exception.py
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
try:
print("Hello")
except:
print("Shouldn't see this")
# Non-exceptional case of try/except/else
try:
print("Hello")
except:
print("Shouldn't see this")
else:
print("Else")
# Exceptional case of try/except
try:
raise Exception("hello")
except:
print("Caught the exception!")
# Non-exceptional case of try/finally
try:
print("Try")
finally:
print("Finally")
# Exceptional case of try/except/finally
try:
raise Exception("Hello")
except:
print("Except")
finally:
print("Finally")
# Try/except handling exception in called function
def f():
raise Exception("uh oh")
print("Shouldn't see this")
try:
f()
except:
print("Caught exception from f()")
# Try/finally with exception raised in called function
def g():
try:
print("Begin")
raise Exception("boom")
finally:
print("End")
try:
g()
except:
print("Caught")
finally:
print("Cleaned up!")
def h():
try:
print("Begin")
raise Exception("boom")
except:
print("Safe!")
finally:
print("End")
try:
h()
except:
print("Not caught here")
else:
print("this is run")
finally:
print("Cleaned up!")
# Testing that return statement peels off exception handlers
def test_handlers_removed_after_return():
try:
return 10
except:
print("you should never see me")
try:
print(test_handlers_removed_after_return())
raise Exception("test")
except:
print("Should only see me")
# Testing that break in an exception handler removes handlers
def test_handlers_removed_after_break():
while True:
try:
break
except:
print("you should never see me")
try:
print(test_handlers_removed_after_break())
raise Exception("test")
except:
print("Should only see me")
# Testing that continue in an exception handler removes handlers
def test_handlers_removed_after_continue():
i = 0
while i == 0:
try:
i = i + 1
continue
except:
print("you should never see me")
try:
print(test_handlers_removed_after_continue())
raise Exception("test")
except:
print("Should only see me")
# Testing that finally is run when return is used
def test_finally_with_return():
try:
print("Before return")
return "return"
finally:
print("Finally")
print(test_finally_with_return())
# Test that finally is run when break is used
def test_finally_with_break():
while True:
try:
print("Before break")
break
finally:
print("Finally")
print(test_finally_with_break())
# Test that finally is run when continue is used
def test_finally_with_continue():
i = 0
while i == 0:
try:
print("Before continue")
i = i + 1
continue
finally:
print("Finally")
print(test_finally_with_continue())
# Do we handle exceptions raised in the exception handler?
def test_raise_in_exception_handler():
try:
print("Before first raise")
raise Exception("oops")
except:
print("Handling")
raise Exception("again")
try:
test_raise_in_exception_handler()
except:
print("Second exception caught")
# Do we handle exceptions raised in the finally block?
def test_raise_in_finally():
try:
print("Before first raise")
raise Exception("oops")
finally:
print("Handling")
raise Exception("again")
try:
test_raise_in_finally()
except:
print("Second exception caught")
# Exception handling should unwind to the try
def test_unwind():
a = 42
print(a)
raise Exception("goodbye!")
a = 14
try:
test_unwind()
except:
print(a)
# Exceptions raised within exception handlers require that the finally block be run
def test_finally_block_run_when_raising_in_exception_handler():
try:
print("Before first raise")
raise Exception("oops")
except:
print("Except")
raise Exception("Not again!")
finally:
print("Finally")
try:
test_finally_block_run_when_raising_in_exception_handler()
except:
print("Second exception caught")
# Exceptions raised within finally blocks should be handled properly
def test_finally_block_raises_exception():
try:
print("Before raise")
raise Exception("oops")
except:
print("Caught")
raise Exception("Uh oh!")
finally:
print("Finally")
raise Exception("ARGHHH")
print("Shouldn't see me")
try:
print("test_finally_block_raises_exception()")
test_finally_block_raises_exception()
except:
print("All done")
# Handler with same class should match
try:
raise Exception("test")
except Exception:
print("Caught")
# Handler with base class should match
try:
raise Exception("test")
except BaseException:
print("Caught")
# Should skip incorrect handlers
try:
raise Exception("test")
except TypeError:
print("TypeError")
except NameError:
print("NameError")
except Exception:
print("Exception")
except BaseException:
print("BaseException")
# Should assign the exception to the name given by the handler
try:
raise Exception("test")
except Exception as e:
print(e)
# Test that the correct handler is located, even if it exists in outer scopes
def test_caught_by_outside_handlers():
try:
raise Exception("hmm")
except TypeError:
print("TypeError")
try:
test_caught_by_outside_handlers()
except Exception:
print("Caught")
# Test that re-raising exceptions works as it should
def test_reraise():
try:
raise Exception("hmm")
except Exception:
print("Inside catch")
raise
try:
test_reraise()
except Exception:
print("Caught again")
def test_reraise_catchall():
try:
raise Exception("hmm")
except:
print("Inside catch")
raise
try:
test_reraise_catchall()
except Exception:
print("Caught again")
# Test break contained in try
try:
while True:
print("About to break")
break
finally:
print("OK!")
# Test continue contained in try
try:
i = 0
while i < 3:
i += 1
print("Loop")
if i == 2:
print("2!")
continue
finally:
print("OK!")
# Test return with while in try
def test_return_in_while():
try:
while True:
return 42
finally:
print("Inner finally")
try:
print(test_return_in_while())
finally:
print("OK!")
# Test that only objects can be raised
try:
raise 3
except TypeError as e:
print("TypeError1")
# Test that only classes derived from BaseException can be raised
class T:
def __str__(self):
return "T"
try:
raise T("e")
except TypeError:
print("TypeError2")
# Test that only classes derived from BaseException can be caught
try:
try:
raise Exception("test")
except T:
pass
except TypeError:
print("TypeError3")
except:
print("caught something else")