Quiz

Traceback Detective

Pros read tracebacks from the bottom up:

  1. Last line: the exception type and message. This is what happened.
  2. The frames above it: the call chain, outermost first. The frame just above the message is where it happened. Look for the first frame in your code, not a library's.
  3. ~~~^^^ markers (Python 3.11+) point at the exact part of the line that failed.

Chained exceptions show two tracebacks:

  • "The above exception was the direct cause of the following exception": someone deliberately wrapped it with raise New(...) from err.
  • "During handling of the above exception, another exception occurred": a second bug happened inside an except block.

In both cases the first traceback is usually the root cause.

Traceback (most recent call last):
  File "config.py", line 4, in read_port
    return int(raw)
           ~~~^^^^^
ValueError: invalid literal for int() with base 10: 'eighty'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "app.py", line 12, in <module>
    port = read_port(env)
  File "config.py", line 6, in read_port
    raise ConfigError("LANTERN_PORT must be a number") from err
ConfigError: LANTERN_PORT must be a number

What actually went wrong in the traceback above?