Skip to main content

How to Handle Different Errors Differently in Python with Several except Blocks

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
try:
<code that may fail>
except <FirstErrorType>:
<handle the first kind>
except <SecondErrorType>:
<handle the second kind>
PartWhat it means
Each except <ErrorType>:A handler for one kind of error
The order of the blocksPython 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​

several_except.py
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")

The several_except.py script in a macOS-style code window: a prices dictionary with a tent and a stove, and a loop over three item and people pairs that prints the price per person inside try, with one except block for KeyError and another for ZeroDivisionError.

  • prices[item] / people can fail in two ways: the item isn't in prices, or people is 0.
  • except KeyError handles the missing item.
  • except ZeroDivisionError handles the division by zero.

Step 2: Run It​

Terminal
uv run python several_except.py
Output
tent costs 30.0 per person
lamp isn't on the price list
stove can't be shared by 0 people

The output in a terminal: tent costs 30.0 per person, lamp isn't on the price list, and 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​

MistakeWhat happensDo this instead
except Exception above except KeyErrorThe broad block matches first, so the KeyError block never runsPut 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 programAdd a block for it, or fix the value before the try

Troubleshooting​

SymptomLikely causeFix
A specific except block never runsA broader block above it catches the error firstMove 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​