import sys import traceback def raise_exception_impl(exi): if exi: raise exi[0], exi[1], exi[2] raise "An exception" def raise_exception(exi=None): raise_exception_impl(exi) def catch1(): try: raise_exception() except: print "catched 1" traceback.print_exc() return sys.exc_info() def catch2(exi): try: raise_exception(exi) except: print "catched 2" traceback.print_exc() print "\n" print_exi(sys.exc_info()) def print_exi(exi): print "Printing traceback..." tb=exi[2] while tb is not None: f1=tb.tb_frame print "Frame1 ", f1 traceback.print_stack(f1,None, sys.stdout) if tb.tb_next is not None: f2=tb.tb_next.tb_frame.f_back print "Frame2 ", f2 if f1 != f2: print "!!!!! Error: expected frame" traceback.print_stack(f2,1, sys.stdout) print "!!!!! Error: end" tb=tb.tb_next def doit(): global_exi=catch1() print "Saved exception 1\n" print_exi(global_exi) print "\nRethrowing exception" catch2(global_exi) doit()