Part 3: Python Programming
13. Exceptions & Error Handling
Exceptions & Error Handling
Things go wrong: a file is missing, a user types "cat" where a number was expected, a calculation divides by zero. When Python hits such a problem it raises an exception, and unless you handle it, your program stops with an error message. This chapter shows how to catch exceptions and respond gracefully.
Why handle errors?
An unhandled exception crashes your program and shows a scary traceback to the user. Handling errors lets your program recover, ask the user to try again, or save their work. Consider this crash:
try and except
Wrap risky code in a try block. If an exception occurs, Python jumps to the matching except block instead of crashing:
It is best to catch the specific exception you expect, like ValueError, rather than catching everything. That way genuine, unexpected bugs still surface instead of being silently swallowed.
Common exception types
| Exception | Happens when... | Example |
|---|---|---|
ValueError | Right type, wrong value | int("abc") |
TypeError | Wrong type used | "a" + 1 |
ZeroDivisionError | Dividing by zero | 5 / 0 |
IndexError | List index out of range | [1, 2][5] |
KeyError | Missing dictionary key | {}["x"] |
FileNotFoundError | Opening a missing file | open("no.txt") |
else and finally
A full error-handling statement can have four parts. The else block runs only if no exception occurred. The finally block always runs, whether or not there was an error, which is ideal for cleanup:
Catching more than one exception
You can handle several exception types, each with its own response, or group them:
Raising your own exceptions
Sometimes your own code discovers a problem and should signal it. Use the raise keyword to raise an exception with a helpful message. This lets callers handle the problem the same way they handle built-in errors:
Writing except ValueError as e captures the exception object in e, so you can print its message. A common real-world use is validating user input: ask again until the value makes sense.
Good error handling is the difference between a fragile program and a robust one. Catch the specific errors you expect, give the user a clear message, and let unexpected bugs surface so you can fix them.