Skip to content
Home / Fundamentals

How to catch multiple exceptions in a single line in Python

It is possible to catch multiple exceptions in a single line in Python using the except clause. Here is an example:

try:
    # some code here
except (Exception1, Exception2, Exception3) as e:
    # code to handle the exceptions

In this example, the code inside the try block will be executed. If an exception of type Exception1, Exception2, or Exception3 is raised, the code inside the except block will be executed. The variable e will contain the exception object that was raised.

You can also specify multiple exceptions in separate except clauses, like this:

try:
    # some code here
except Exception1 as e:
    # code to handle Exception1
except Exception2 as e:
    # code to handle Exception2
except Exception3 as e:
    # code to handle Exception3

It is also possible to catch all exceptions by using the Exception class without specifying any specific exception types. For example:

try:
    # some code here
except Exception as e:
    # code to handle any exception

Here is a complete example that demonstrates how to catch multiple exceptions in a single line:

def divide(a, b):
    try:
        result = a / b
    except (ZeroDivisionError, TypeError) as e:
        print(e)
        result = None
    return result

print(divide(10, 2))  # prints 5.0
print(divide(10, 0))  # prints division by zero
print(divide(10, 'a'))  # prints unsupported operand type(s) for /: 'int' and 'str'

In this example, the divide function takes two arguments and divides the first by the second. If a ZeroDivisionError or a TypeError is raised, the exception is caught and the value None is returned. If no exceptions are raised, the result of the division is returned.

It is important to note that the order in which the exceptions are specified in the except clause is important. Python will evaluate the exceptions from left to right, and the first matching exception will be caught. For example:

try:
    # some code here
except ZeroDivisionError as e:
    # code to handle ZeroDivisionError
except TypeError as e:
    # code to handle TypeError

In this case, if a ZeroDivisionError is raised, it will be caught by the first except clause and the code inside that clause will be executed. If a TypeError is raised, it will be caught by the second except clause. If a different exception is raised, it will not be caught by either clause and will propagate up the call stack.

It is also possible to use the else clause in a try-except block to specify code that should be executed if no exceptions are raised. For example:

try:
    # some code here
except (Exception1, Exception2, Exception3) as e:
    # code to handle the exceptions
else:
    # code to be executed if no exceptions are raised

Finally, the finally clause can be used to specify code that should be executed whether or not an exception is raised. This is useful for cleaning up resources or closing open files, for example. For example:

try:
    # some code here
except (Exception1, Exception2, Exception3) as e:
    # code to handle the exceptions
finally:
    # code to be executed whether or not an exception is raised