Error Handling & Debugging: Don't Panic at Red Text
After this lesson, you will be able to:
- Read Python error messages confidently — they tell you exactly what went wrong and where
- Use try/except to handle errors gracefully instead of letting your program crash
- Raise your own errors with clear messages when something goes wrong
- Debug code using print statements and a systematic 4-step process
Before You Start
#Common Python Errors
#Python Errors Are Helpful, Not Scary
When Python shows red text, it's not yelling at you — it's telling you exactly what went wrong. Let's learn to read it.
#The 3 Most Common Beginner Errors
#NameError: "I don't know this variable"
print(username)
# NameError: name 'username' is not definedusername = "alex" # ← define it first
print(username) # ✅ Now it worksWhat error does this raise? usernme = "alex" print(username)
usernme (missing 'a'), but line 2 tries to read username, which Python has never seen. Python doesn't auto-correct typos; it raises NameError: name 'username' is not defined. The error message names the EXACT misspelled identifier, which is your big clue. 90% of NameErrors are typos in variable names — the other 10% are using a variable before it was assigned, or forgetting to import a moduleModules & PackagesA module is a .py file you import to reuse code. Use import, from...import, and aliases (import numpy as np). Packages are directories with __init__.py.Learn more →.#TypeError: "Wrong type for this operation"
age = "25"
print(age + 1)
# TypeError: can only concatenate str (not "int") to str"25" is a string (text), 1 is a number — you can't add them directly.age = int("25") # ← convert to integer first
print(age + 1) # ✅ 26#IndexError: "That position doesn't exist"
fruits = ["apple", "banana", "cherry"]
print(fruits[5])
# IndexError: list index out of rangeprint(fruits[2]) # ✅ "cherry" — the last element
print(fruits[-1]) # ✅ Also "cherry" — negative indices count from end#Reading a Traceback
When errors happen inside functions, Python shows a full traceback:
Traceback (most recent call last):
File "app.py", line 12, in main
result = calculate(data)
File "app.py", line 7, in calculate
return data[0] / data[1]
ZeroDivisionError: division by zero
Read bottom-up
ZeroDivisionError: division by zero← this is your answer — you divided by zeroFile "app.py", line 7, in calculate← happened in thecalculatefunction, line 7return data[0] / data[1]← this specific line caused it
The bottom line tells you what. The lines above trace back how you got there.
#try/except: Handle Errors Gracefully
Without error handling, one bad input crashes your entire program. With try/except, you catch the error and respond sensibly.
This code calls input(). Python here can't pause to ask you, so type the answers below before running — one value per line, in the order the program asks for them.
# Without error handling — crashes on bad input
age = int(input("Enter your age: ")) # ← crashes if user types "abc"
# With error handling — graceful recovery
try:
age = int(input("Enter your age: "))
print(f"You are {age} years old")
except ValueError:
print("That's not a valid number. Please enter a digit like 25.")What gets printed? try: print("A") x = 10 / 0 print("B") except ZeroDivisionError: print("C") print("D")
#Catching Multiple Error Types
This code calls input(). Python here can't pause to ask you, so type the answers below before running — one value per line, in the order the program asks for them.
try:
number = int(input("Enter a number: "))
result = 100 / number
print(f"100 / {number} = {result}")
except ValueError:
print("Please enter a valid number")
except ZeroDivisionError:
print("Can't divide by zero!")
except Exception as e:
print(f"Something unexpected happened: {e}")#try / except / else / finally
try:
file = open("data.txt", "r")
content = file.read()
except FileNotFoundError:
print("File not found — check the filename")
else:
# Runs ONLY if no exception occurred
print(f"File loaded: {len(content)} characters")
finally:
# ALWAYS runs — perfect for cleanup
print("Done attempting to read file")What prints, in what order? try: x = int("abc") except ValueError: print("caught") finally: print("cleanup")
#Raising Your Own Errors
Sometimes you want to raise an error when something is logically wrong — even if Python wouldn't crash on its own.
def calculate_age(birth_year: int) -> int:
if birth_year < 1900 or birth_year > 2026:
raise ValueError(f"Birth year {birth_year} doesn't seem right")
return 2026 - birth_year
try:
age = calculate_age(1850)
except ValueError as e:
print(f"Error: {e}") # "Error: Birth year 1850 doesn't seem right"#The 4-Step Debugging Process
When your code doesn't work:
- Read the error message — bottom-up, find the exact line and error type
- Check that line — look for typos, wrong types, wrong variable names
- Add print statements. Print intermediate values to see what's happening
- Rubber duck it. Explain your code out loud, step by step. You'll often spot the bug yourself.
# Debugging with print — add temporarily, remove when fixed
def process_data(items):
print(f"DEBUG: items = {items}") # ← what did we receive?
total = 0
for item in items:
print(f"DEBUG: item = {item}, type = {type(item)}") # ← what is each item?
total += item
print(f"DEBUG: total = {total}") # ← what are we returning?
return totalInteractive Lab
Step through code line by line and watch exactly where errors occur
Key Takeaways
- Read Python error messages bottom-up — the last line tells you what went wrong, the lines above show where
- The 3 most common errors: NameError (undefined variable), TypeError (wrong type), IndexError (position out of range)
- try/except lets your program handle errors gracefully instead of crashing — always specify the exception type
- Debug by adding print() statements to check intermediate values — delete them when the bug is fixed
This program is supposed to catch ONLY bad number inputs and print a friendly message. Instead, a bare except: also hides a typo in the print line, so every call silently returns None. Fix the handler so it catches the right exception, and fix the typo it was hiding.
Bad age: twelve None 25
A user types 'hello' when your code expects a number and runs int('hello'). What error occurs?