@@ -147,10 +147,10 @@ For example, the following code will print B, C, D in that order::
147147Note that if the *except clauses * were reversed (with ``except B `` first), it
148148would have printed B, B, B --- the first matching *except clause * is triggered.
149149
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)::
154154
155155 import sys
156156
@@ -162,10 +162,13 @@ the exception (allowing a caller to handle the exception as well)::
162162 print("OS error: {0}".format(err))
163163 except ValueError:
164164 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)=}" )
167167 raise
168168
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+
169172The :keyword: `try ` ... :keyword: `except ` statement has an optional *else
170173clause *, which, when present, must follow all *except clauses *. It is useful
171174for 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. ::
493496After the statement is executed, the file *f * is always closed, even if a
494497problem was encountered while processing the lines. Objects which, like files,
495498provide predefined clean-up actions will indicate this in their documentation.
496-
497-
0 commit comments