I know that I can do:
try:
# do something that may fail
except:
# do this if ANYTHING goes wrong
I can also do this:
try:
# do something that may fail
except IDontLikeYouException:
# say please
except YouAreTooShortException:
# stand on a ladder
But if I want to do the same thing inside two different exceptions, the best I can think of right now is to do this:
try:
# do something that may fail
except IDontLikeYouException:
# say please
except YouAreBeingMeanException:
# say please
Is there any way that I can do something like this (since the action to take in both exceptions is to say please
):
try:
# do something that may fail
except IDontLikeYouException, YouAreBeingMeanException:
# say please
Now this really won't work, as it matches the syntax for:
try:
# do something that may fail
except Exception, e:
# say please
So, my effort to catch the two distinct exceptions doesn't exactly come through.
Is there a way to do this?
From Python documentation -> 8.3 Handling Exceptions:
One of the way to do this is..
and another way is to create method which performs task executed by
except
block and call it through all of theexcept
block that you write..I know that second one is not the best way to do this, but i'm just showing number of ways to do this thing.
For python 2.5 and earlier versions, the correct syntax is:
Where
e
is the Exception instance.From Python Documentation:
Or, for Python 2 only:
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
.Do this:
The parentheses are required due to older syntax that used the commas to assign the error object to a name. The
as
keyword is used for the assignment. You can use any name for the error object, I prefererror
personally.Best Practice
To do this in a manner currently and forward compatible with Python, you need to separate the Exceptions with commas and wrap them with parentheses to differentiate from earlier syntax that assigned the exception instance to a variable name by following the Exception type to be caught with a comma.
Here's an example of simple usage:
I'm specifying only these exceptions to avoid hiding bugs, which if I encounter I expect the full stack trace from.
This is documented here: https://docs.python.org/tutorial/errors.html
You can assign the exception to a variable, (
e
is common, but you might prefer a more verbose variable if you have long exception handling or your IDE only highlights selections larger than that, as mine does.) The instance has an args attribute. Here is an example:Note that in Python 3, the
err
object falls out of scope when theexcept
block is concluded.Deprecated
You may see code that assigns the error with a comma. This usage, the only form available in Python 2.5 and earlier, is deprecated, and if you wish your code to be forward compatible in Python 3, you should update the syntax to use the new form:
If you see the comma name assignment in your codebase, and you're using Python 2.5 or higher, switch to the new way of doing it so your code remains compatible when you upgrade.
The
suppress
context managerThe accepted answer is really 4 lines of code, minimum:
The
try
,except
,pass
lines can be handled in a single line with the suppress context manager, available in Python 3.4:So when you want to
pass
on certain exceptions, usesuppress
.Python 2.7 Documentation states that: