running code if try statements were successful in python

Posted by None on Stack Overflow See other posts from Stack Overflow or by None
Published on 2010-05-08T01:22:10Z Indexed on 2010/05/08 1:28 UTC
Read the original article Hit count: 338

I was wondering if in python there was a simple way to run code if a try statement was successful that wasn't in the try statement itself. Is that what the else or finally commands do (I didn't understand their documentation)? I know I could use code like this:

successful = False
try:
    #code that might fail
    successful = True
except:
    #error handling if code failed
if successful:
    #code to run if try was successful that isn't part of try

but I was wondering if there was a shorter way .

© Stack Overflow or respective owner

Related posts about python

Related posts about try-statement