Playground / Reading a Traceback

Walk the frames from where it failed

Reading a Traceback

Interactive lab

Try it: Reading a Traceback

How to read a Python traceback: the last line names the error type and message, the frame at the bottom is where Python discovered the problem, each frame above is a call that led there, and the root cause is often earlier than the reported line, in the data passed down the calls.

How it works

  1. Execution runs line by line; each function call pushes a new frame onto the call stack.
  2. When an operation fails (words[0] on an empty list, 'a' + 5, int('x9'), d['missing'], x / 0, an undefined name, None.upper()), Python raises an exception at that line.
  3. With no try/except, the exception leaves the frame and returns to the caller at the line that made the call; that call becomes the next entry above in the traceback.
  4. CPython prints "Traceback (most recent call last):", one File/line/function entry per frame from the outermost call down to the failing line, with ^^^ / ~~~^^^ markers under the failing expression, then "Type: message".
  5. Read it from the bottom (what failed and where) upwards (how execution got there), then look for where the bad value came from: that is the cause; the reported line is the symptom.

Default run (20 steps): First words: the value on the last line is "". Press Play to run it and watch for the error. … Uncaught: CPython prints the traceback and stops. Read it from the bottom: IndexError: list index out of range was discovered at line 3 in first_word; the lines above show how execution got there.

Simplified: The lab's own small Python interpreter runs fixed, hand-written programs (you pick the program, the fix and a value; your text is never executed). Every program the lab can show was run in CPython 3.12 and the lab reproduces its output, its traceback text and its line-by-line trace. Seven short programs; you choose the value on the last line, which makes the error appear or disappear.

Educational simulation

Loading the simulation…