forked from trekhleb/learn-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_handle_exceptions.py
106 lines (83 loc) · 3.8 KB
/
test_handle_exceptions.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
"""Errors and Exceptions.
@see: https://docs.python.org/3/tutorial/errors.html#errors-and-exceptions
Even if a statement or expression is syntactically correct, it may cause an error when an attempt
is made to execute it. Errors detected during execution are called exceptions and are not
unconditionally fatal.
It is possible to write programs that handle selected exceptions.
"""
def test_handle_exceptions():
"""Handling of exceptions
The try statement works as follows.
- First, the try clause (the statement(s) between the try and except keywords) is executed.
- If no exception occurs, the except clause is skipped and execution of the try statement
is finished.
- If an exception occurs during execution of the try clause, the rest of the clause is skipped.
Then if its type matches the exception named after the except keyword, the except clause is
executed, and then execution continues after the try statement.
- If an exception occurs which does not match the exception named in the except clause, it is
passed on to outer try statements; if no handler is found, it is an unhandled exception and
execution stops with a message.
"""
# Let's simulate division by zero exception.
exception_has_been_handled = False
try:
result = 10 * (1 / 0) # division by zero
# We should not get here at all.
assert result
except ZeroDivisionError:
# We should get here because of division by zero.
exception_has_been_handled = True
assert exception_has_been_handled
# Let's simulate undefined variable access exception.
exception_has_been_handled = False
try:
# pylint: disable=undefined-variable
result = 4 + spam * 3 # name 'spam' is not defined
# We should not get here at all.
assert result
except NameError:
# We should get here because of division by zero.
exception_has_been_handled = True
assert exception_has_been_handled
# A try statement may have more than one except clause, to specify handlers for different
# exceptions. At most one handler will be executed. Handlers only handle exceptions that occur
# in the corresponding try clause, not in other handlers of the same try statement. An except
# clause may name multiple exceptions as a parenthesized tuple, for example:
exception_has_been_handled = False
try:
result = 10 * (1 / 0) # division by zero
# We should not get here at all.
assert result
except (ZeroDivisionError, NameError):
# We should get here because of division by zero.
exception_has_been_handled = True
assert exception_has_been_handled
# Exception handlers may be chained.
exception_has_been_handled = False
try:
result = 10 * (1 / 0) # division by zero
# We should not get here at all.
assert result
except NameError:
# We should get here because of division by zero.
exception_has_been_handled = True
except ZeroDivisionError:
# We should get here because of division by zero.
exception_has_been_handled = True
assert exception_has_been_handled
# The try … except statement has an optional else clause, which, when present, must follow all
# except clauses. It is useful for code that must be executed if the try clause does not raise
# an exception. For example:
exception_has_been_handled = False
no_exceptions_has_been_fired = False
try:
result = 10
# We should not get here at all.
assert result
except NameError:
# We should get here because of division by zero.
exception_has_been_handled = True
else:
no_exceptions_has_been_fired = True
assert not exception_has_been_handled
assert no_exceptions_has_been_fired