Errors and debugging
Professional programmers spend more time reading and fixing code than writing it. Errors aren't failures: they're the computer telling you exactly what's wrong. Learning to read them calmly is a superpower.
Three kinds of errors
| Kind | When | Example |
|---|---|---|
| Syntax error | Before running: the code isn't valid | A missing bracket or quote |
| Runtime error | While running: something impossible happens | Dividing by zero, calling something undefined |
| Logic error (bug) | The program runs but gives the wrong answer | Using > instead of >= |
Logic errors are the sneakiest, because there's no error message at all.
How to read an error message
Read it from the bottom up:
- The last line is the error type and message:
ZeroDivisionError: division by zero. What went wrong. - The lines above form the traceback: the chain of function calls, ending at the exact line where it broke. Where it went wrong.
- Ask yourself why:
len([])is 0, so we divided by zero. The fix is to handle the empty case.
TypeError: Cannot read properties of undefined (reading 'city') means user.address is undefined, so you can't get .city from it. One of the most common JavaScript errors ever.
Common errors decoded
| Error | Usually means |
|---|---|
SyntaxError |
A typo in the structure: brackets, quotes, colons, indentation |
NameError (Py) / ReferenceError (JS) |
You used a name that doesn't exist (a typo, or out of scope) |
TypeError |
Wrong type: calling a non-function, or adding text to a number |
IndexError / undefined from an array |
You asked for a position past the end of a list |
KeyError (Py) |
That key isn't in the dictionary |
IndentationError (Py) |
Inconsistent spaces or tabs |
Copy the exact error message into a search engine (minus your own variable names). Millions of developers have seen it before.
The debugging process
- Reproduce it. Find exact steps that trigger the bug every time.
- Read the error and find the line.
- Form a hypothesis: "I think
totalis wrong because…" - Test it: inspect values with print statements or a debugger.
- Fix one thing at a time, then re-test.
- Write a test so the bug can't come back.
Technique 1: print debugging
The simplest and most-used tool. Print the values at key points to see where reality differs from your expectation:
The debug line reveals the bug: percent is 10, not 0.10. Fix it with price * percent / 100.
Technique 2: the debugger
Every serious editor has a debugger. You set a breakpoint on a line, and the program pauses there so you can inspect every variable and step through line by line.
- Browser JavaScript: DevTools → Sources tab, or write
debugger;in your code - VS Code: click left of a line number to add a breakpoint (red dot), then press F5
- Python:
breakpoint()pauses in the terminal debugger (pdb)
| Debugger button | Does |
|---|---|
| Continue | Run until the next breakpoint |
| Step over | Run the current line, and don't go into function calls |
| Step into | Go inside the function being called |
| Step out | Finish the current function and return to the caller |
Technique 3: rubber duck debugging 🦆
Explain your code, line by line, out loud, to anyone or anything (a rubber duck works). It sounds silly, but explaining forces you to look at what the code actually does instead of what you think it does. Very often you'll spot the bug halfway through a sentence.
Technique 4: divide and conquer
If a big program is broken, cut the problem in half: comment out half the code, or check the value in the middle. Is it still wrong? Then the bug is in the first half. Repeat. git bisect automates this across your commit history.
Handling errors gracefully
Some errors are expected, like bad user input or a network failure. Catch them instead of crashing:
Practice: find and fix the bugs
This function should return the average of the positive numbers in a list. It has three bugs. Use print debugging to find them.
1.Where should you start reading a Python traceback?
2.The program runs without errors but shows the wrong total. What kind of error is this?