300

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

try:
 #something1
 #something2
except ExceptionType1:
 #return xyz
except ExceptionType2:
 #return abc
polvoazul
  • 1,638
  • 1
  • 16
  • 25
Eva611
  • 5,349
  • 8
  • 29
  • 38
  • Presumably something1 is an exception class in the line `except something1`? – Steve Mayne May 23 '11 at 10:12
  • 2
    @Sentinel - Fair enough. Apologize if I abused SO community. – Eva611 May 23 '11 at 10:24
  • 9
    @Eva611: Don't apologize. (1) try it. (2) post an answer to your question. – S.Lott May 23 '11 at 11:11
  • 6
    It does work. Tried it with the python interpreter... – Eva611 May 23 '11 at 11:18
  • 2
    "Try-it-and-see" is definitely a better first port of call than SO - being able to easily try things at the interactive prompt is a major advantage that scripting languages have over compiled languages. – ncoghlan May 23 '11 at 11:35
  • 54
    @Eva611 I think your question was fine. Was much faster for me to Google this rather than setting up an example in the Python interpreter, so I was happy that you asked. DrTysa and others should have just responded with a quick "yes" rather than scolding you. – galarant Apr 22 '13 at 19:48
  • 4
    // , How is this not a real question? Seems like a useful y/n to have an answer for, to me. – Nathan Basanese Jan 23 '16 at 22:19
  • 9
    Most of the time your first guess is going to be wrong, or miss subtle nuances, so (like 15,000 others) I tried google first! – Chris Jan 26 '16 at 17:57
  • 15
    I tried google and found this question. Therefore, I'm glad someone else asked it in so public a forum as SO, and I'm glad enough other people linked to it that google's algorithm popped it up as my first result. – kingledion Jul 27 '16 at 19:36

1 Answers1

481

Yes, it is possible.

try:
   ...
except FirstException:
   handle_first_one()

except SecondException:
   handle_second_one()

except (ThirdException, FourthException, FifthException) as e:
   handle_either_of_3rd_4th_or_5th()

except Exception:
   handle_all_other_exceptions()

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)

vartec
  • 125,354
  • 36
  • 211
  • 241
  • 110
    If you want to do the same thing in both cases, it's `except (SomeError, OtherError):`. Doesn't answer the OP question, but might help some people who get here through Google. – Mark Sep 25 '13 at 14:43
  • If for example you have to convert multiple versions of a data structure to a new structure, when updating versions of code for example, you can nest the try..excepts. – Rolf of Saxony Dec 04 '15 at 14:00
  • 7
    If you want to handle all exceptions you should be using `except Exception:` instead of plain `except:`. (Plain except will catch even `SystemExit` and `KeyboardInterrupt` which usually is not what you want) – polvoazul Feb 06 '18 at 19:08
  • 1
    You perhaps want to do something with `e` also since you give it a name :) – HelloGoodbye Mar 25 '20 at 00:21
  • If you just one to avoid getting error without handling specific exceptions, you can write nested levels of try/except as mentioned also in this [answer](https://stackoverflow.com/a/36755323/5127304). – Elias Aug 13 '21 at 10:53