Reading a Python Traceback

Use the exception type, reported line, and chain of calls to find the inputs behind a failed operation.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
2 min
In this article4 chapters
  1. Start at the failing call
  2. Work backward from the value
  3. Reconstruct the operation from its callers
  4. Pay attention to exception chaining

Start at the failing call

Read the exception type and message near the end. The nearest listed frame in that traceback usually identifies the operation that raised the exception. Earlier frames show the calling path. When exceptions are chained, inspect both tracebacks and their messages. Syntax and runtime failures have different starting points for investigation.

Work backward from the value

Suppose a program fails on prices["milk"] inside charge(order). A KeyError tells you the requested key is absent from that mapping. Look at the call and learn whether the input used "Milk", whether the catalog was incomplete, or whether the caller chose the wrong mapping. A trace of program state that includes the actual key and the map at that call can distinguish these possibilities.[1][2]

Reconstruct the operation from its callers

Suppose a report calls format_item(row). That helper calls unit_price(row["kind"]). The price lookup raises KeyError for the key "tea". Starting from the final error text, note the missing key and the frame containing the lookup. An earlier frame tells you which operation supplied that key. Go up one more level to identify which report row was selected. The sequence of frames answers both “where did the error arise?” and “how did this input reach it?”

A missing lookup can have several ordinary causes. The export may include a category that the catalogue never contained. The loader may have discarded a valid category because it interpreted a heading incorrectly. A caller may also have passed a different catalogue. The traceback gives you a route for inspection. A small reproduction retaining one problematic row and a small catalogue lets you test those alternatives.

Pay attention to exception chaining

A loader can catch a low-level parse error and raise a field-level diagnostic with more context. In that case the displayed chain may contain both the original error and the translated one. Use the innermost relevant operation to explain what failed, and use the enclosing operation to explain its meaning to a caller. Exception propagation across calls describes the layers between them.

Write down four facts before changing code: the error type, the lowest relevant line, the value read at that line, and the caller responsible for providing it. For a report played through two entry points, preserve which one reproduced the issue. That check can prevent fixing one caller while another still supplies the wrong value.

References

  1. Python Software Foundation, Python Tutorial: Errors and Exceptions ↩
  2. Python Software Foundation, pdb: The Python Debugger ↩