@@ -101,29 +101,29 @@ The :keyword:`try` statement works as follows.
101
101
* If no exception occurs, the *except clause * is skipped and execution of the
102
102
:keyword: `try ` statement is finished.
103
103
104
- * If an exception occurs during execution of the try clause, the rest of the
105
- clause is skipped. Then if its type matches the exception named after the
106
- :keyword: `except ` keyword, the except clause is executed, and then execution
107
- continues after the :keyword: ` try ` statement .
104
+ * If an exception occurs during execution of the :keyword: ` try ` clause, the rest of the
105
+ clause is skipped. Then, if its type matches the exception named after the
106
+ :keyword: `except ` keyword, the * except clause * is executed, and then execution
107
+ continues after the try/except block .
108
108
109
- * If an exception occurs which does not match the exception named in the except
110
- clause, it is passed on to outer :keyword: `try ` statements; if no handler is
109
+ * If an exception occurs which does not match the exception named in the * except
110
+ clause * , it is passed on to outer :keyword: `try ` statements; if no handler is
111
111
found, it is an *unhandled exception * and execution stops with a message as
112
112
shown above.
113
113
114
- A :keyword: `try ` statement may have more than one except clause, to specify
114
+ A :keyword: `try ` statement may have more than one * except clause * , to specify
115
115
handlers for different exceptions. At most one handler will be executed.
116
- Handlers only handle exceptions that occur in the corresponding try clause, not
117
- in other handlers of the same :keyword: `!try ` statement. An except clause may
118
- name multiple exceptions as a parenthesized tuple, for example::
116
+ Handlers only handle exceptions that occur in the corresponding * try clause *,
117
+ not in other handlers of the same :keyword: `!try ` statement. An * except clause *
118
+ may name multiple exceptions as a parenthesized tuple, for example::
119
119
120
120
... except (RuntimeError, TypeError, NameError):
121
121
... pass
122
122
123
123
A class in an :keyword: `except ` clause is compatible with an exception if it is
124
124
the same class or a base class thereof (but not the other way around --- an
125
- except clause listing a derived class is not compatible with a base class). For
126
- example, the following code will print B, C, D in that order::
125
+ * except clause * listing a derived class is not compatible with a base class).
126
+ For example, the following code will print B, C, D in that order::
127
127
128
128
class B(Exception):
129
129
pass
@@ -144,10 +144,10 @@ example, the following code will print B, C, D in that order::
144
144
except B:
145
145
print("B")
146
146
147
- Note that if the except clauses were reversed (with ``except B `` first), it
148
- would have printed B, B, B --- the first matching except clause is triggered.
147
+ Note that if the * except clauses * were reversed (with ``except B `` first), it
148
+ would have printed B, B, B --- the first matching * except clause * is triggered.
149
149
150
- The last except clause may omit the exception name(s), to serve as a wildcard.
150
+ The last * except clause * may omit the exception name(s), to serve as a wildcard.
151
151
Use this with extreme caution, since it is easy to mask a real programming error
152
152
in this way! It can also be used to print an error message and then re-raise
153
153
the exception (allowing a caller to handle the exception as well)::
@@ -167,9 +167,9 @@ the exception (allowing a caller to handle the exception as well)::
167
167
raise
168
168
169
169
The :keyword: `try ` ... :keyword: `except ` statement has an optional *else
170
- clause *, which, when present, must follow all except clauses. It is useful for
171
- code that must be executed if the try clause does not raise an exception. For
172
- example::
170
+ clause *, which, when present, must follow all * except clauses * . It is useful
171
+ for code that must be executed if the * try clause * does not raise an exception.
172
+ For example::
173
173
174
174
for arg in sys.argv[1:]:
175
175
try:
@@ -189,7 +189,7 @@ When an exception occurs, it may have an associated value, also known as the
189
189
exception's *argument *. The presence and type of the argument depend on the
190
190
exception type.
191
191
192
- The except clause may specify a variable after the exception name. The
192
+ The * except clause * may specify a variable after the exception name. The
193
193
variable is bound to an exception instance with the arguments stored in
194
194
``instance.args ``. For convenience, the exception instance defines
195
195
:meth: `__str__ ` so the arguments can be printed directly without having to
@@ -217,8 +217,8 @@ If an exception has arguments, they are printed as the last part ('detail') of
217
217
the message for unhandled exceptions.
218
218
219
219
Exception handlers don't just handle exceptions if they occur immediately in the
220
- try clause, but also if they occur inside functions that are called (even
221
- indirectly) in the try clause. For example::
220
+ * try clause * , but also if they occur inside functions that are called (even
221
+ indirectly) in the * try clause * . For example::
222
222
223
223
>>> def this_fails():
224
224
... x = 1/0
0 commit comments