How to catch multiple exceptions in one line? (in the “except” block)

From Python Documentation:

An except clause may name multiple exceptions as a parenthesized tuple, for example

except (IDontLikeYouException, YouAreBeingMeanException) as e:

Or, for Python 2 only:

except (IDontLikeYouException, YouAreBeingMeanException), e:

Separating the exception from the variable with a comma will still work in Python 2.6 and 2.7, but is now deprecated and does not work in Python 3; now you should be using as.

Leave a Comment