Skip to main content Brad's PyNotes

Posts on Exceptions

  1. Built-in Exceptions

    TL;DR

    Python’s built-in exceptions form a strict inheritance hierarchy rooted in BaseException, with all user code exceptions inheriting from Exception. Choose specific exception types like ValueError (wrong value) or TypeError (wrong type) rather than generic exceptions, and consider carefully before catching BaseException in user code as it will do things like capture keyboard interrupts (no Ctrl-C for you!).

  2. Tutorial: Errors and Exceptions

    TL;DR

    Python separates syntax errors (caught before running) from exceptions (caught during execution). The try/except/finally system lets you catch errors, recover gracefully, and guarantee cleanup code runs.

  3. PEP 3134: Exception Chaining and Embedded Tracebacks

    TL;DR

    PEP 3134 introduced exception chaining in Python 3, allowing exceptions to preserve their context when new exceptions occur during error handling. The raise ... from syntax creates explicit chains, while Python automatically captures implicit chains, both appearing in tracebacks to simplify debugging.