Skip to main content Brad's PyNotes

Posts on Error-Handling

  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. 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.