@@ -147,10 +147,10 @@ For 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)::
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)::
154
154
155
155
import sys
156
156
@@ -162,10 +162,13 @@ the exception (allowing a caller to handle the exception as well)::
162
162
print("OS error: {0}".format(err))
163
163
except ValueError:
164
164
print("Could not convert data to an integer.")
165
- except:
166
- print("Unexpected error:", sys.exc_info()[0] )
165
+ except BaseException as err :
166
+ print(f "Unexpected {err=}, {type(err)=}" )
167
167
raise
168
168
169
+ Alternatively the last except clause may omit the exception name(s), however the exception
170
+ value must then be retrieved from ``sys.exc_info()[1] ``.
171
+
169
172
The :keyword: `try ` ... :keyword: `except ` statement has an optional *else
170
173
clause *, which, when present, must follow all *except clauses *. It is useful
171
174
for code that must be executed if the *try clause * does not raise an exception.
@@ -493,5 +496,3 @@ used in a way that ensures they are always cleaned up promptly and correctly. ::
493
496
After the statement is executed, the file *f * is always closed, even if a
494
497
problem was encountered while processing the lines. Objects which, like files,
495
498
provide predefined clean-up actions will indicate this in their documentation.
496
-
497
-
0 commit comments