[Solved] Python: One Try Multiple Except

In Python, is it possible to have multiple except statements for one try statement? Such as :

except ExceptionType1:
 #return xyz
except ExceptionType2:
 #return abc
Enquirer: Eva611

Solution #1:

Yes, it is possible.

except FirstException:

except SecondException:

except (ThirdException, FourthException, FifthException) as e:

except Exception:

See: http://docs.python.org/tutorial/errors.html

The “as” keyword is used to assign the error to a variable so that the error can be investigated more thoroughly later on in the code. Also note that the parentheses for the triple exception case are needed in python 3.

This page has more info: Catch multiple exceptions in one line (except block)

Respondent: vartec
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.