classification
Title: incorrect example
Type: Stage: resolved
Components: Documentation Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Alistair, docs@python, martin.panter, serhiy.storchaka
Priority: normal Keywords:

Created on 2018-12-09 12:40 by Alistair, last changed 2018-12-11 22:16 by eric.snow. This issue is now closed.

Messages (3)
msg331428 - (view) Author: Alistair Lenhard (Alistair) Date: 2018-12-09 12:40
under: https://docs.python.org/3/tutorial/errors.html

Original it says:
"Note that if the except clauses were reversed (with except B first), it would have printed B, B, B — the first matching except clause is triggered."

It should read:
"Note that if the except clauses were reversed (with except B first), it would have printed D, D, D — the first matching except clause is triggered."
 
As D is the first expression in the print statement.
So if the expression is changed to "except B:"

class B(Exception):
    pass

class C(B):
    pass

class D(C):
    pass

for cls in [B, C, D]:
    try:
        raise cls()
    except B:
        print("D")
    except C:
        print("C")
    except B:
        print("B")

Result is:
D
D
D
msg331479 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2018-12-10 09:42
It doesn’t make sense to move the “except” line without moving the matching “print” line. According to <https://docs.python.org/3.7/reference/compound_stmts.html#index-1>, “A clause consists of a header and a ‘suite’.” So when it talks about reversing the “except” clauses, that includes reversing the corresponding “print” lines, so they condinue to identify which exception handler was triggered.
msg331480 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-12-10 10:05
The example is correct. If the except clauses were reversed

for cls in [B, C, D]:
    try:
        raise cls()
    except B:
        print("B")
    except C:
        print("C")
    except D:
        print("D")

it would have printed B, B, B.
History
Date User Action Args
2018-12-11 22:16:44eric.snowsetpull_requests: - pull_request10353
2018-12-11 22:14:08eric.snowsetpull_requests: + pull_request10353
2018-12-10 10:05:09serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg331480

resolution: not a bug
stage: resolved
2018-12-10 09:42:41martin.pantersetnosy: + martin.panter
messages: + msg331479
2018-12-09 12:40:06Alistaircreate