python / python/cpython

chapter 8.3 in python errors-tutorial: update exception order code example for clarification

Aperta
#136,228 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

docs
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Documentation

The errors tutorial, chapter 8.3 contains an example that could be improved, as the highlighted subjects (exception ordering and matching) are not that easy to follow: https://docs.python.org/3/tutorial/errors.html

The issue was discussed here:
https://discuss.python.org/t/exception-tutorial-confusion/97456

Original:
A class in an except clause matches exceptions which are instances of the class itself or one of its derived classes (but not the other way around — an except clause listing a derived class does not match instances of its base classes). For example, the following code will print B, C, D in that order:

class B(Exception):
    pass

class C(B):
    pass

class D(C):
    pass

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

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.

Suggested Update
Code examples created by Guido van Rossum, see https://discuss.python.org/t/exception-tutorial-confusion/97456/5.
A class in an except clause matches exceptions which are instances of the class itself or one of its derived classes (but not the other way around — an except clause listing a derived class does not match instances of its base classes).

For example, the following code will print B, C in that order:

class B(Exception):
    pass

class C(B):
    pass

for cls in [B, C]:
    try:
        raise cls()
    except C:
        # Matches C but not B.
        print("C")
    except B:
        # Matches B; not reached for C.
        print("B")

Note that if the except clauses were reversed (with except B first), it would have printed B, B — the first matching except clause is triggered, like in the following example:

for cls in [B, C]:
    try:
        raise cls()
    except B:
        # Matches B and C both.
        print("B")
    except C:
        # Not reached (the previous clause ate B and C)
        print("C")
Linked PRs
  • gh-136257

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Leggi il capitolo 8.3 del tutorial sugli errori nella pagina della documentazione collegata e rivedi la discussione relativa al chiarimento proposto. Confronta gli esempi attuali sull'ordine delle eccezioni con gli esempi suggeriti; il lavoro è completato quando il capitolo dimostra chiaramente il matching e l'ordine della prima corrispondenza.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.