File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -129,29 +129,38 @@ may name multiple exceptions as a parenthesized tuple, for example::
129129A class in an :keyword: `except ` clause matches exceptions which are instances of the
130130class 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
153151Note 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
156165When an exception occurs, it may have associated values, also known as the
157166exception's *arguments *. The presence and types of the arguments depend on the
You can’t perform that action at this time.
0 commit comments