The following script shows how an exception is caught and how the traceback information is retrieved and printed.
#!/usr/bin/env python import traceback import sys def funcCrash(): a = 1/0 return try: funcCrash() except Exception as e: print( "catching the exception") _, _, tb = sys.exc_info() traceback.print_tb(tb) tb_info = traceback.extract_tb(tb) filename, line, func, text = tb_info[-1] print( ">>> File %s, line %s, in %s \n %s" % (filename, line, func, text))
This is the output:
catching the exception File "./trace.py", line 11, in <module> funcCrash() File "./trace.py", line 7, in funcCrash a = 1/0 >>> File ./trace.py, line 7, in funcCrash a = 1/0