Using finally clauses. : finally « Statement « Python Tutorial






def doNotRaiseException():
   try:
      print "In doNotRaiseException"
   finally:
      print "Finally executed in doNotRaiseException"

   print "End of doNotRaiseException"

def raiseExceptionDoNotCatch():
   try:
      print "In raiseExceptionDoNotCatch"
      raise Exception
   finally:
      print "Finally executed in raiseExceptionDoNotCatch"

   print "Will never reach this point"

print "Calling doNotRaiseException"
doNotRaiseException()

print "\nCalling raiseExceptionDoNotCatch"

try:
   raiseExceptionDoNotCatch()
except Exception:
   print "Caught exception from raiseExceptionDoNotCatch in main program."








3.11.finally
3.11.1.Using finally clauses.
3.11.2.Defining Clean-Up Actions
3.11.3.The finally clause will execute, whether there was an error encountered or not.