1
Try: 
     #some statement 
    Try: 
         #some statement 
    Except: 
         #statement1 
        Raise exception()
       #statement2 
Except: #some statement

Can I pass the control like the above code in python., will the inner except pass the control to the outer except and will the #statement2 be executed?

1
  • 3
    Just a side note: they don't have capital letters. Regarding your question: Why don't you test it? Commented Feb 22, 2015 at 13:03

1 Answer 1

4

This code will answer your question:

#!/usr/bin/env python
import sys
try:
    try:
        raise Exception("first exception")
    except Exception as e:
        print e.message
        raise Exception("second exception")
        print "second statement" # never printed - 'dead code'
except Exception as e:
    print e.message

Both except blocks are executed but the statement after raising the second exception is not.

Generally you should know that once an exception is raised, nothing executes until it is caught by an except block that is relevant to this exception or any superclass of it.

Sign up to request clarification or add additional context in comments.

2 Comments

The #statement2 mentioned in the question isn't addressed here
@keyser added a more relevant example, thanks for the comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.