Exceptions and Debugging
Level 5 · Errors and debugging
What an exception is, and how to catch one with try and except. How to read a stack trace from the bottom, and how to hunt down a bug.
Course contentsLesson 16 of 18
Level 1 · What a program is
Level 2 · Values and variables
Level 3 · Decisions and loops
Level 4 · Functions
Level 5 · Errors and debugging
Level 6 · Good code habits
After this lesson you can
- Catch an exception with try and except
- Read a stack trace to find the failing line
- Narrow down a bug with print and a debugger
14 mincovers 3 glossary entries
Worth reading first: Bugs and Error Types
How to read an error report and find the bug
An exception is the signal Python raises when something breaks while the program runs. Each kind has a name that says what went wrong, such as KeyError for a missing key. If nothing deals with it, the program stops and prints a report. Python has dozens of these kinds built in.
You can catch an exception so the program does not crash. Put the risky line inside try:, and the plan B inside except ValueError:. If the line works, the except block is skipped. If it raises that error, Python jumps straight to the except block and carries on from there.
Catch only the error you expect, and only where you can do something useful about it. A bare except: catches everything, including bugs you needed to see. A wrong number typed by a user is worth catching. A typo in your own code is not.
When an exception is not caught, Python prints a stack trace. It starts with Traceback (most recent call last): and lists every function call that led to the failing line. Read it from the bottom. The last line names the error, and the line just above it shows where it happened.
Debugging is the work of finding why code goes wrong, then fixing it. Start by making the bug happen again, every time. Then narrow down where it lives. The quickest tool is a print line that shows a value, placed halfway through. If the value is already wrong there, the bug is above it.
A debugger does the same job with more control. It pauses the program at a line you choose, called a breakpoint. Then you can step through one line at a time and see every variable as it changes. Python ships with one, called pdb, and most code editors build one in.
Some bugs fall to explaining the code out loud, line by line, to anyone, even a rubber duck. Saying what each line really does shows the gap between that and what you thought it did. The common mistake is guessing and changing lines at random. Narrow it down first.
text = input("How many people? ")
try:
people = int(text)
print("Each pays", 60 / people)
except ValueError:
print("Please type a whole number.")
except ZeroDivisionError:
print("There must be at least 1 person.")Worked example
Jack reads his first stack trace
Jack's app crashes and prints 9 lines. He reads from the top and is lost in function names he never wrote.
Then he reads from the bottom. The last line says KeyError: 'price', and the line above points to line 42 of cart.py. One item in the data had no price, and he fixes it there.
The words for this lesson3
Check yourself
Where should you start reading a Python stack trace?
Finished this lesson?
Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.