Demonstrating exception arguments and stack unwinding. : except « Statement « Python Tutorial






import traceback

def function1():
   function2()

def function2():
   function3()

def function3():
   # raise exception, catch exception, reraise exception
   try:
      raise Exception, "An exception has occurred"
   except Exception:
      print "Caught exception in function3. Reraising....\n"
      raise   # reraises most recent exception

# call function1, any Exception it generates will be
# caught by the except clause that follows
try:
   function1()

# output exception arguments, string representation of exception,
# and the traceback
except Exception, exception:
   print "Exception caught in main program."
   print "\nException arguments:", exception.args
   print "\nException message:", exception
   print "\nTraceback:"
   traceback.print_exc()








3.10.except
3.10.1.Catching Two Exceptions
3.10.2.Simple exception handling example.
3.10.3.Demonstrating exception arguments and stack unwinding.
3.10.4.Catch more than on exceptions
3.10.5.Catch KeyError and AssertionError
3.10.6.try/except block that checks for correct user input
3.10.7.try/except block with string argument