Skip to content

Commit 52fd75f

Browse files
committed
document BaseException in favour of bare except
1 parent 802726a commit 52fd75f

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

Doc/tutorial/errors.rst

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,10 @@ 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)::
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)::
156154

157155
try:
158156
f = open('myfile.txt')
@@ -162,10 +160,13 @@ the exception (allowing a caller to handle the exception as well)::
162160
print("OS error: {0}".format(err))
163161
except ValueError:
164162
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)
167165
raise
168166

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+
169170
The :keyword:`try` ... :keyword:`except` statement has an optional *else
170171
clause*, which, when present, must follow all except clauses. It is useful for
171172
code that must be executed if the try clause does not raise an exception. For

0 commit comments

Comments
 (0)