SyntaxStudy
Sign Up
Python Catching Multiple Exceptions
Python Beginner 3 min read

Catching Multiple Exceptions

Catching Multiple Exceptions

You can handle different exception types with multiple except clauses, or group them in a tuple.

Example
try:
    value = int(input("Enter a number: "))
    result = 100 / value
except ValueError:
    print("Not a valid number")
except ZeroDivisionError:
    print("Cannot divide by zero")
Pro Tip

Order except blocks from most specific to most general.