How to Catch Multiple Exceptions in Python
Sometimes the same piece of Python code can fail in more than one way.
For example:
- converting text to a number can raise a
ValueError - dividing by that number can raise a
ZeroDivisionError
In cases like this, you can catch multiple exceptions with one except block.
This page shows you:
- how to catch more than one exception in a
tryblock - the correct syntax for grouped exceptions
- when to use one
exceptblock - when separate
exceptblocks are better
Quick fix
try:
number = int(user_input)
result = 10 / number
except (ValueError, ZeroDivisionError) as e:
print(f"Error: {e}")
Use parentheses to catch multiple exception types in one except block.
What this page helps you do
- Catch more than one exception in a
tryblock - Use one
exceptblock for multiple error types - Decide when to use separate
exceptblocks instead - Avoid common beginner syntax mistakes
When catching multiple exceptions is useful
Catching multiple exceptions is useful when one block of code can fail in different expected ways.
Common examples:
- A single line of code can fail in different ways
- User input often causes more than one kind of error
- File handling and type conversion commonly need this pattern
- It helps keep error handling short and readable
For a general introduction to exception handling, see how to use try and except blocks in Python.
Basic syntax
Use try for code that might fail.
Use except (ErrorType1, ErrorType2): to catch several exceptions in one block.
Example
user_input = "0"
try:
number = int(user_input)
result = 10 / number
print(result)
except (ValueError, ZeroDivisionError):
print("Please enter a valid non-zero number.")
How it works
try:contains the risky codeint(user_input)may raiseValueError10 / numbermay raiseZeroDivisionError- the
exceptblock catches either error
If you want to see the actual error message, use as e.
user_input = "hello"
try:
number = int(user_input)
result = 10 / number
except (ValueError, ZeroDivisionError) as e:
print(f"Error: {e}")
Single except block for multiple exceptions
Use one except block when the response should be the same for all the errors you expect.
This is best when:
- the fix is the same
- the message to the user is the same
- you want to avoid repeating code
Example
user_input = input("Enter a number: ")
try:
number = int(user_input)
result = 100 / number
print("Result:", result)
except (ValueError, ZeroDivisionError):
print("Please enter a valid number that is not zero.")
This works well because both errors lead to the same advice:
- if the input is not a number, the user should enter a valid number
- if the input is zero, the user should enter a non-zero number
Using separate except blocks
Use separate except blocks when each error needs a different message or different handling.
This is often clearer when:
- the solution depends on the error type
- you want more helpful feedback
- you are debugging and need to know exactly what failed
Example
user_input = input("Enter a number: ")
try:
number = int(user_input)
result = 100 / number
print("Result:", result)
except ValueError:
print("That was not a valid number.")
except ZeroDivisionError:
print("You cannot divide by zero.")
This version is better if you want specific feedback.
If you are still learning the full structure of exception handling, see using try, except, else, and finally in Python.
How Python checks except blocks
Python checks except blocks from top to bottom.
The first matching block runs.
Example
try:
number = int("hello")
except ValueError:
print("This is a ValueError.")
except Exception:
print("This catches many other errors.")
Output:
This is a ValueError.
Python stops at the first match.
Important rule
Put more specific exceptions before broader ones.
Good:
try:
number = int("hello")
except ValueError:
print("Invalid number")
except Exception:
print("Something else went wrong")
Less helpful:
try:
number = int("hello")
except Exception:
print("Something went wrong")
except ValueError:
print("Invalid number")
In the second example, except ValueError will never run because Exception catches it first.
A broad except Exception can hide useful details if you place it too early. Beginners should usually catch specific exceptions first. You can learn more in how to handle exceptions in Python.
Common beginner mistakes
Forgetting parentheses
This is correct:
try:
number = int("hello")
except (ValueError, ZeroDivisionError):
print("Handled error")
This is not correct in modern Python:
try:
number = int("hello")
except ValueError, ZeroDivisionError:
print("Handled error")
When you catch multiple exception types in one block, put them inside parentheses.
Catching Exception too early
This catches too much:
try:
number = int("hello")
except Exception:
print("Error")
It works, but it hides which error you expected.
This is usually better:
try:
number = int("hello")
except ValueError:
print("Please enter a valid number.")
If you want to understand this error better, see ValueError in Python: causes and fixes.
Grouping exceptions that need different handling
Do not group exceptions just because you can.
Bad fit:
try:
number = int(input("Enter a number: "))
result = 10 / number
except (ValueError, ZeroDivisionError):
print("Error")
Better:
try:
number = int(input("Enter a number: "))
result = 10 / number
except ValueError:
print("Please enter digits only.")
except ZeroDivisionError:
print("Zero is not allowed.")
If you need help with division errors, see ZeroDivisionError: division by zero fix.
When not to catch multiple exceptions together
Do not group exceptions if that makes your code less clear.
Avoid grouping errors together when:
- the user needs different guidance for each one
- you might hide a real programming mistake
- simple input validation would solve the problem first
- you are catching errors you do not understand
For example, if you know a value should be checked before division, input validation may be clearer than relying only on exceptions.
Common causes
You will often need this pattern in situations like these:
- Converting user input with
int()when the input is not a number - Dividing by zero after converting input
- Opening files when the file may not exist or access may fail
- Calling code that can raise more than one expected exception
If int() is part of your code, you may also want to read the Python int() function explained.
Debugging tips
If you are not sure which exception happened, inspect the error object.
user_input = "hello"
try:
number = int(user_input)
result = 10 / number
except (ValueError, ZeroDivisionError) as e:
print(type(e))
print(e)
Useful debugging commands:
print(type(e))
print(e)
help(ValueError)
help(ZeroDivisionError)
These help you learn:
- the exact exception type
- the error message
- what the exception means
FAQ
How do you catch two exceptions in Python?
Use one except block with parentheses:
except (ValueError, ZeroDivisionError):
Can I use as e when catching multiple exceptions?
Yes.
Example:
except (ValueError, ZeroDivisionError) as e:
Should I use one except block or separate ones?
Use one block if the response is the same.
Use separate blocks if each error needs a different message or fix.
Can I catch all errors with Exception?
Yes, but beginners should prefer specific exceptions first so errors are easier to understand and debug.