All courses

Reading an error message and stack trace

Find the error type, the message, and the file and line where a failure happened in a stack trace. Identify which part to paste to an agent as evidence.

Builds on: Command output and exit codes

Read it card by card Start the whole course

A wall of red text: what matters?

You run a script and, instead of a total, you get this: ``` $ python report.py Traceback (most recent call last): File "report.py", line 8, in <module> total = add_prices(rows) File "src/prices.py", line 5, in add_prices total += r["price"] KeyError: 'price' ``` What went wrong, and where? If you ask an agent for help, what exactly do you show it?

A trace is the chain of calls at the crash

Programs are split into **functions**, named blocks of code that call each other. When a program crashes, Python prints a **traceback** (also called a **stack trace**). It lists the chain of function calls in progress at that moment. Python lists them oldest first and newest last, hence "most recent call last". Here, `report.py` line 8 called `add_prices`, which was running line 5 of `src/prices.py` when it failed. Each `File ... line ... in ...` entry plus its code line is one **frame**. The last line is the verdict: the error type, then the message.

Four things to pull out of any trace

1. **Error type + message**: Python puts it on the last line. JavaScript and Java put it near the top, just above the frames, and list the newest call first. 2. **Where it broke**: the frame nearest the error line. `report.js:18:3` means line 18, column 3. 3. **Your own code**: skip library frames, which are code your project uses but didn't write (paths with `site-packages`, `node_modules`, `node:internal`). Start with the frame from your own project that is nearest the error line. 4. **Evidence**: the command you ran, the whole trace copied exactly, and what you expected.

Reading the KeyError trace

Type: `KeyError`, meaning code asked a dictionary for a key it doesn't have. Message: `'price'` is the missing key. It broke in `src/prices.py` line 5, inside `add_prices`, which was called from `report.py` line 8. So some row has no `"price"` entry. When you ask for help, don't write "report crashes". Don't retype "key error" either. Paste the command and the full trace verbatim, and say what you expected.

What you paste to the agent

The command, what you expected, and the trace exactly as printed. Exact file names and line numbers let the agent open the right spot. A loose summary makes it guess.
▸ You: `python report.py` crashes. I
expected it to print the total price.
Full output:
Traceback (most recent call last):
  File "report.py", line 8, in <module>
    total = add_prices(rows)
  File "src/prices.py", line 5, in add_prices
    total += r["price"]
KeyError: 'price'
Find the cause before changing code.

Remember

In Python the error type and message sit on the last line, with the newest call just above. In JavaScript or Java they sit near the top, above the frames, with the newest call first. Each frame gives a file, line and function. Look first at the frame from your own code nearest the error line. As evidence, paste the command plus the whole trace copied exactly, and say what you expected.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.