How to Handle Different Errors Differently in Python with Several except Blocks
The same few lines of code can fail in different ways, and each way needs its own answer: a missing item needs one message, a division by zero another. A try can have several except blocks, one for each kind of error. This guide handles two different errors in a small price calculator, with real output from Python 3.15.2.
Syntax
try:
<code that may fail>
except <FirstErrorType>:
<handle the first kind>
except <SecondErrorType>:
<handle the second kind>
| Part | What it means |
|---|---|
Each except <ErrorType>: | A handler for one kind of error |
| The order of the blocks | Python checks them top to bottom and runs the first one that matches |
Python's tutorial: "A try statement may have more than one except clause, to specify handlers for different exceptions. At most one handler will be executed." It adds that "the first matching except clause is triggered."
Prerequisites
- uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
prices = {"tent": 120, "stove": 45}
for item, people in [("tent", 4), ("lamp", 2), ("stove", 0)]:
try:
print(item, "costs", prices[item] / people, "per person")
except KeyError:
print(item, "isn't on the price list")
except ZeroDivisionError:
print(item, "can't be shared by 0 people")

prices[item] / peoplecan fail in two ways: the item isn't inprices, orpeopleis 0.except KeyErrorhandles the missing item.except ZeroDivisionErrorhandles the division by zero.
Step 2: Run It
uv run python several_except.py
tent costs 30.0 per person
lamp isn't on the price list
stove can't be shared by 0 people

The tent worked. The lamp raised KeyError, so the first handler ran. The stove raised ZeroDivisionError, which skipped the KeyError block and ran the second one. Each error got its own message, and the loop kept going.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
except Exception above except KeyError | The broad block matches first, so the KeyError block never runs | Put the most specific errors first |
An error none of the blocks names, such as dividing by "2" | It isn't caught: TypeError: unsupported operand type(s) for /: 'int' and 'str' stops the program | Add a block for it, or fix the value before the try |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
A specific except block never runs | A broader block above it catches the error first | Move the specific block up |
FAQ
Can one block handle two error types?
Yes. Name both in one except, such as except (KeyError, ValueError):, when they need the same answer. Separate blocks are for different answers.
Does the code after the try still run?
Yes, once a handler has dealt with the error: here, the loop went on to the next item each time.
Conclusion
Two except blocks gave two answers: the lamp's KeyError said it isn't on the price list, and the stove's ZeroDivisionError said it can't be shared by 0 people.
Additional Resources
- Handling Exceptions —
try,exceptand the order of handlers. - Exception hierarchy — which errors are subclasses of which.
