Tracebacks & try/except
beginner12 min readLesson 27 of 169
The three error families, reading a traceback bottom-up, and catching exceptions narrowly.
Not all bugs are equal. Python tells you which kind you have โ learning to read that message is the skill.
The three error families
Syntax errors โ Python cannot even start. It points at the line it chokes on:
if x > 5
print("big")
# SyntaxError: expected ':'
Runtime errors (exceptions) โ the program starts, then crashes mid-run:
ages = {"minh": 21}
print(ages["linh"])
# KeyError: 'linh'
Logic errors โ the program runs to completion and gives the WRONG answer. No error message appears. These are the most dangerous, and the reason testing exists:
average = a + b / 2 # runs fine โ and is wrong
The traceback: read it bottom-up
Traceback (most recent call last):
File "report.py", line 4, in <module>
total = sum(values) / count
ZeroDivisionError: division by zero
Read the LAST line first: the error type and message. Then the line above: where it happened. Then follow the call chain upward to find what led there.