Reading Tracebacks
Unit ID: M06-U01 Estimated active time: 22-30 minutes
A traceback shows the call path
Consider:
def parse_hours(raw_hours):
return int(raw_hours.strip())
def prepare_record(record):
hours = parse_hours(record["hours"])
return {"id": record["id"], "hours": hours}
prepare_record({"id": "R1", "hours": "six"})
The final exception is ValueError. The traceback also shows that the call moved from prepare_record() into parse_hours() and failed at int(...).
Read from the bottom, then move upward
- Read the exception type and message at the bottom.
- Find the innermost failing line.
- Move upward to see which function supplied the value.
- Inspect the smallest relevant inputs.
The innermost line shows where Python detected the failure. The cause may be an earlier assumption about the input.
Preserve the full error
Do not report only "it did not work". Record:
ValueError: invalid literal for int() with base 10: 'six'
The exact message helps another person reproduce and distinguish the failure.
A KeyError path
prepare_record({"id": "R2"})
This fails earlier at record["hours"] with KeyError. It is a different failure and needs a different decision: is the field required, optional, or invalid?
Practice
Run the two failing examples separately. For each, record exception type, message, failing line, caller, input, and likely violated assumption.
Takeaway
A traceback is a structured path, not noise. Start with the final exception, find the innermost operation, and trace the value back through callers. Next, we will reduce a failure to the smallest useful example.
