Skip to content

Commit e9a6f1b

Browse files
authored
bpo-41576: document BaseException in favor of bare except (GH-21917)
1 parent 0ffdced commit e9a6f1b

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

Doc/tutorial/errors.rst

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ For example, the following code will print B, C, D in that order::
147147
Note that if the *except clauses* were reversed (with ``except B`` first), it
148148
would 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+
169172
The :keyword:`try` ... :keyword:`except` statement has an optional *else
170173
clause*, which, when present, must follow all *except clauses*. It is useful
171174
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. ::
493496
After the statement is executed, the file *f* is always closed, even if a
494497
problem was encountered while processing the lines. Objects which, like files,
495498
provide predefined clean-up actions will indicate this in their documentation.
496-
497-
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
document BaseException in favor of bare except

0 commit comments

Comments
 (0)