Kodokon kodokon.com

Reading and understanding an error message

Learn to decode an error message instead of closing it: type, file, line and stack trace.

8 min · 3 questions

Open this lesson in Kodokon

An error is not a punishment. Your computer is not judging you and it is not laughing at you: it has noticed that it does not know what to do, and it is telling you why. It works exactly like a GPS announcing "recalculating route" - that is not a reproach, it is information. The beginner's reflex is to close the red window without reading it. The developer's reflex is to read it slowly, all the way to the end. It is often the only moment when the machine speaks to you in plain words.

Almost every error message contains the same pieces of information. The type of error, which sorts it into a broad family. The message, which explains in one sentence what is wrong. The file involved and the line number, which tell you exactly where to look. And sometimes a stack trace: the list of steps the program went through before breaking down.

PYTHON
Traceback (most recent call last):
  File "shopping.py", line 12, in <module>
    total = price * quantity
NameError: name 'quantity' is not defined
A Python message: the file, line 12, the NameError type and its explanation.

In the example above, everything is spelled out. The file is shopping.py, the line is 12, the type is NameError and the message says name 'quantity' is not defined. In plain English: on line 12 you are using a variable quantity that you never created, or whose name you misspelled. No complicated investigation needed - the message already contained the answer.

A Python stack trace reads from the bottom up: the last line gives the type and the explanation, and just above it sit the file and the line at fault. When the trace is very long, look for the first line that mentions your own file. Lines pointing at unfamiliar files with strange names belong to the libraries you are using: 99% of the time they are not the ones with a bug, the way you are calling them is.

JAVASCRIPT
Uncaught TypeError: Cannot read properties of
undefined (reading 'name')
    at showProfile (app.js:24:18)
    at start (app.js:41:3)
Same logic in the browser console: file app.js, line 24, column 18.

A handful of phrasings come up again and again, and it pays to recognize them. not defined means the name you used does not exist at that point. undefined means the thing exists but holds no value. unexpected means the program was expecting something else, typically a forgotten bracket or quote. permission denied points to a rights problem, not a code problem. And no such file or directory means the path you wrote leads nowhere.

Knowledge check

Make sure you remember the key points of this lesson.

  1. Which part of an error message tells you where to look first?
    • The file name and the line number
    • The color of the text on screen
    • The total length of the message
    • The name of your operating system
  2. Which way do you read a Python stack trace?
    • From the top down: the first line gives the solution
    • From the bottom up: the last line gives the type and the explanation
    • Either way, it is purely decorative
  3. This program stops on NameError: name 'count' is not defined. Which name do you write in the blank to fix the error?
    price = 10
    ___ = 3
    total = price * count
    print(total)
    • count
    • total
    • price
    • number