Skip to main content

How to Catch Several Exceptions with One except in Python

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Sometimes two different errors mean the same thing to your program, such as "this isn't a number", and you want to handle them in one place. One except clause can name several exception types. This guide catches two in a small conversion function, with real output from Python 3.15.2.

Syntax​

Syntax
try:
...
except <ExceptionType1>, <ExceptionType2>:
... # runs for either type

# with a name for the exception, the parentheses are required:
except (<ExceptionType1>, <ExceptionType2>) as <name>:
...
PartWhat it means
<ExceptionType1>, <ExceptionType2>The types this clause handles. Without parentheses, this form needs Python 3.14 or newer
( ... ) as <name>The same types in parentheses, with the caught exception bound to <name>. Works in every Python 3 version

What's New in Python 3.14: "The except and except* expressions now allow brackets to be omitted when there are multiple exception types and the as clause is not used."

Prerequisites​

  • uv and Python 3.14 or newer. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

to_number turns text into a whole number. int("forty-two") raises ValueError, and int(None) raises TypeError; the function answers None for both.

to_number.py
def to_number(text):
try:
return int(text)
except ValueError, TypeError:
return None


print(to_number("42"))
print(to_number("forty-two"))
print(to_number(None))

The to_number.py script in a macOS-style code window: a to_number function with try return int(text) and except ValueError, TypeError returning None, then three print calls with "42", "forty-two" and None.

Step 2: Run It​

Terminal
uv run python to_number.py
Output
42
None
None

The script's output in a terminal: 42, None and None.

"42" converted; the other two raised different errors, and the one except handled both.

Common Mistakes​

MistakeWhat happensDo this instead
Adding as error without parenthesesSyntaxError: multiple exception types must be parenthesized when using 'as'except (ValueError, TypeError) as error:
Writing two except clauses with the same bodyIt works, but the code is repeatedOne clause that names both types

Troubleshooting​

SymptomLikely causeFix
SyntaxError on except ValueError, TypeError:Python 3.13 or older. Python's parser, set to the 3.13 grammar, said except expressions without parentheses are only supported in Python 3.14 and greaterAdd parentheses: except (ValueError, TypeError):

FAQ​

How do I see which error it was?​

Bind it with as, in parentheses. except (ValueError, TypeError) as error: with print("Not a number:", error) printed Not a number: int() argument must be a string, a bytes-like object or a real number, not 'NoneType' for None.

Is the parenthesized form still valid?​

Yes. The parentheses are optional only when there's no as; the parenthesized form works in every version.

Conclusion​

except ValueError, TypeError: handled both errors in one place, and to_number answered None for "forty-two" and for None. Add parentheses when you also want the exception with as.

Additional Resources​