Python: One Try Multiple Except
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Breezy Bay
--
Chapters
00:00 Python: One Try Multiple Except
00:21 Accepted Answer Score 573
00:59 Thank you
--
Full question
https://stackoverflow.com/questions/6095...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #syntax #exceptionhandling
#avk47
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Breezy Bay
--
Chapters
00:00 Python: One Try Multiple Except
00:21 Accepted Answer Score 573
00:59 Thank you
--
Full question
https://stackoverflow.com/questions/6095...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #syntax #exceptionhandling
#avk47
ACCEPTED ANSWER
Score 579
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)
ANSWER 2
Score 0
Nested try except could work too.
I found the above solution was hard to implement and I just had a single extra exception.
try:
1/0
except:
try:
1/0
except:
1/1