Catch multiple exceptions in one Python handler

Use a tuple of exception classes when failures share a recovery path, and keep version differences and exception inheritance explicit.

Best practices
Illustration of software components, data, and application workflows

Use except (ValueError, ZeroDivisionError) as exc: to handle either exception in the same block. The tuple means “any of these types,” including their subclasses; exc is the particular exception instance that was raised.

Share a handler when recovery is the same

def divide_input(user_input, divisor):
    try:
        return int(user_input) / divisor
    except (ValueError, ZeroDivisionError) as exc:
        print(f"Cannot calculate the result: {exc}")
        return None

This catches invalid integer text and division by zero. It deliberately does not catch every possible error: for example, passing an unsupported object to int() can raise TypeError.

Separate different recovery paths

You can inspect isinstance(exc, ValueError) inside a shared handler, but separate clauses are usually easier to read when the actions differ:

def explain_input(user_input, divisor):
    try:
        return int(user_input) / divisor
    except ValueError:
        print("Enter a valid integer.")
    except ZeroDivisionError:
        print("The divisor must not be zero.")

Put specific exception types before a broader parent. Catching OSError, for example, also catches FileNotFoundError and PermissionError; listing every subclass is unnecessary when they genuinely share handling.

A broad except Exception excludes direct BaseException subclasses such as KeyboardInterrupt and SystemExit. It can be appropriate at an application boundary, where logger.exception("Operation failed") inside the handler preserves traceback context. It should not silently turn programming errors into successful results.

FAQ

Can I omit the parentheses?

Python 3.14 permits except ValueError, TypeError: when there is no as binding. With as exc, the tuple still needs parentheses. Python 3.0–3.13 reject the unparenthesized form; Python 2 gave it a different meaning, binding the exception to the second name. Keep the tuple form for code spanning Python 3 versions.

Can I catch everything except selected types?

There is no exclusion operator. Put a specific handler that immediately uses raise before a broader except Exception handler. Bare raise re-raises the active exception. Keep the try block narrow so the fallback catches only failures it can handle.

Is exc a tuple?

No. It is the raised exception object. The tuple lists the classes that the handler accepts.

References: PEP 758: optional exception-handler parentheses, Python exception handling.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting