TactivergeLite

Lite / Building Machines That Respond / Week 8

Read the traceback

Your program crashes with six lines of red text. Most people read the top. Why does the answer start at the bottom?

About 30 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

Error messages are not insults; they are the most useful debugging information you will ever get for free. Engineers who read them calmly fix crashes in seconds instead of hours.

Predict

A traceback ends with ZeroDivisionError: division by zero, and the line above says line 5, in mean. Where is the fault?

  1. Line 1 of the program
  2. Line 5, inside the function mean
  3. In Python itself
Guess first, then reveal

Line 5, inside mean. The last line names the error; the 'File …, line …' entry just above it shows where it happened. Entries higher up show which call led there.

Discover

Work through 'Text plus a number', 'Mean of an empty log' and 'One equals sign'. For each, read the traceback from the bottom, pick the line you suspect and check. Use the print probe to see the variables just before the crash.

What the interactive shows (text version)

The bug hunt shows a short program, what was expected and what actually happened. 'Text plus a number' crashes on line 3 with TypeError: can only concatenate str (not "float") to str. 'Mean of an empty log' crashes on line 5 inside mean, called from line 7, with ZeroDivisionError. 'One equals sign' fails before running with a SyntaxError on line 3, suggesting '=='. Selecting a line and checking explains the cause and the fix.

You can also open the interactive on the full site when you have a faster connection.

Understand

Bottom up

Traceback (most recent call last):
  File "log.py", line 7, in <module>
    print(mean([]))
  File "log.py", line 5, in mean
    return total / len(readings)
ZeroDivisionError: division by zero

Last line: the error type and message. Just above: the file, line and code where it happened. Higher up: the chain of calls that got there (line 7 called mean).

Three kinds of error

Syntax errors stop the program before anything runs: if x = 5: (Python even suggests ==). Runtime errors crash it part-way: dividing by zero, adding text to a number. Logic errors never crash: the program runs and gives a wrong answer. Logic errors are the hardest, so tests matter.

The usual suspects

NameError: a misspelt name or one used before it is assigned. TypeError: the wrong type, such as "23.5" + 0.4. ValueError: right type, bad value, such as float("ERR"). ZeroDivisionError: dividing by zero, often an empty list. IndexError: a list position that does not exist, such as readings[7] in a 7-item list.

Explain it another way

Intuitive

A traceback is a trail of footprints. The crash is at the end of the trail; earlier footprints show how you got there.

Technical

When an exception is not handled, Python unwinds the call stack and prints each frame, oldest first, then the exception type and message. Python 3.11 and later also underline the exact part of the line that failed.

Applied

On robots and servers, tracebacks are written to log files. The first thing an engineer does after a failure is find the last traceback in the log and read it from the bottom.

Worked example 1

A traceback ends: IndexError: list index out of range, at line 4: last = readings[len(readings)]. Explain and fix.

A list of n items has positions 0 to n − 1, so readings[len(readings)] is one past the end. Use readings[len(readings) - 1] or simply readings[-1].

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

Practice 1. Which part of a traceback tells you the kind of error?
Practice 2. Which is a logic error?
Practice 3. float("ERR") raises which error?

Apply it

Write five tiny programs, each causing one of NameError, TypeError, ValueError, ZeroDivisionError and IndexError. Run each and copy its last traceback line into a table with one sentence on the cause and the fix.

Reflect

Which error message did you find hardest to understand, and what does it actually mean now?

Write your answer on paper or in your own notes. Lite never stores what you write.

Go further

← Decompose a program into functionsReproduce, hypothesise, test: hunting logic bugs →