1

I'm working on these try/except/else statements but I have a problem.

I'd like to put a code like this :

try:
   ...
except:
   ...
else:
   ...
else:
   ...

But I know it's impossible to put 2 "else" consecutively.
I tried an "elif" but I read that it doesn't work inside an "except". And it did not work.
So how could I do to make 2 or more than 2 "else" statements ?

Thank you ! Julien

2
  • What are you trying to achieve? At the moment - I'm not sure this is a question... Commented Jan 11, 2013 at 10:02
  • Are you trying to catch two different kinds of exception? e.g. if the block in the try clause could throw either ValueError or TypeError, and you want to have different behaviour depending on which one... Commented Jan 11, 2013 at 10:04

3 Answers 3

6

You will never need two else block. If you need to further distinguish within the else, do:

try:
    ...
except:
    ...  
else:
    if condition:
        ...
    else:
        ...
Sign up to request clarification or add additional context in comments.

Comments

1

Why not simply put everything in one else branch?

Comments

1
try:
   ...
except:
   ...
else:
    if:
    ...
    elif:
    ...
    else:
    ...

If your action is dependent on the exception you get:

try:
   ...
except FirsException:
   #handle_first_one()

except SecondException:
   #handle_second_one()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.