Skip to content

Commit 9991df0

Browse files
committed
gh-136228: Clarify example output in exception handling section of errors.rst
1 parent da79ac9 commit 9991df0

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

Doc/tutorial/errors.rst

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,29 +129,38 @@ may name multiple exceptions as a parenthesized tuple, for example::
129129
A class in an :keyword:`except` clause matches exceptions which are instances of the
130130
class itself or one of its derived classes (but not the other way around --- an
131131
*except clause* listing a derived class does not match instances of its base classes).
132-
For example, the following code will print B, C, D in that order::
132+
133+
For example, the following code will print B, C in that order::
133134

134135
class B(Exception):
135136
pass
136137

137138
class C(B):
138139
pass
139140

140-
class D(C):
141-
pass
142-
143-
for cls in [B, C, D]:
141+
for cls in [B, C]:
144142
try:
145143
raise cls()
146-
except D:
147-
print("D")
148144
except C:
145+
# Matches C but not B.
149146
print("C")
150147
except B:
148+
# Matches B; not reached for C.
151149
print("B")
152150

153151
Note that if the *except clauses* were reversed (with ``except B`` first), it
154-
would have printed B, B, B --- the first matching *except clause* is triggered.
152+
would have printed B, B --- the first matching *except clause* is triggered,
153+
like in the following example::
154+
155+
for cls in [B, C]:
156+
try:
157+
raise cls()
158+
except B:
159+
# Matches B and C both.
160+
print("B")
161+
except C:
162+
# Not reached (the previous clause ate B and C)
163+
print("C")
155164

156165
When an exception occurs, it may have associated values, also known as the
157166
exception's *arguments*. The presence and types of the arguments depend on the

0 commit comments

Comments
 (0)