@@ -147,12 +147,10 @@ example, the following code will print B, C, D in that order::
147
147
Note that if the except clauses were reversed (with ``except B `` first), it
148
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.
151
- Use this with extreme caution, since it is easy to mask a real programming error
152
- in this way! It can also be used to print an error message and then re-raise
153
- the exception (allowing a caller to handle the exception as well)::
154
-
155
- import sys
150
+ All exceptions inherit from :exc: `BaseException `, and so it can be used to serve
151
+ as a wildcard. Use this with extreme caution, since it is easy to mask a real
152
+ programming error in this way! It can also be used to print an error message and
153
+ then re-raise the exception (allowing a caller to handle the exception as well)::
156
154
157
155
try:
158
156
f = open('myfile.txt')
@@ -162,10 +160,13 @@ the exception (allowing a caller to handle the exception as well)::
162
160
print("OS error: {0}".format(err))
163
161
except ValueError:
164
162
print("Could not convert data to an integer.")
165
- except:
166
- print("Unexpected error:", sys.exc_info()[0] )
163
+ except BaseException as err :
164
+ print("Unexpected error:", err )
167
165
raise
168
166
167
+ Alternatively the last except clause may omit the exception name(s), however the exception
168
+ value must then be retrieved from ``sys.exc_info()[0] ``.
169
+
169
170
The :keyword: `try ` ... :keyword: `except ` statement has an optional *else
170
171
clause *, which, when present, must follow all except clauses. It is useful for
171
172
code that must be executed if the try clause does not raise an exception. For
0 commit comments