Traceback module allows you to print abnormal trace return (traceback) information in the program 1.1 Traceback.print_exc ()
file:traceback-example-1.py# note! Importing the Traceback module messes up the# exception state, so you better does that here and not# in the exception handle r# Note! Importing Traceback will clean up the exception state, so it is best not to import the module in the exception handling code imports Tracebacktry: raise SyntaxError, "example" except: Traceback.print_exc () Traceback (innermost last): File "traceback-example-1.py", line 7, in? Syntaxerror:example
1.2 TRACEBACK.EXTRACT_TB
File:traceback-example-3.pyimport tracebackimport sysdef function (): raise IOError, "an I/O error occurred" try: function () except: info = sys.exc_info () for file, Lineno, function, text in TRACEBACK.EXTRACT_TB (info[2]) : print file, "line", Lineno, "in", function print "=", repr (text) print "* *%s:%s"% info[:2]traceback-e xample-3.py line 8 in?=> ' function () ' traceback-example-3.py line 5 in function=> ' Raise IOError, "an I/O error OCCU Rred "' * * exceptions. Ioerror:an I/O error occurred
1.3 Traceback.extract_stack
Import Sys,traceback,osdef Lumberjack (): Bright_side_of_death () def bright_side_of_death (): print Traceback.extract_stack () return 0if __name__ = = ' __main__ ': lumberjack ()
Operation Result:
[' d:\\train\\python\\sample\\traceback\\sample.py ', ' <module> ', ' Lumberjack () '), (' d:\\train\\python\\ Sample\\traceback\\sample.py ', 4, ' Lumberjack ', ' Bright_side_of_death () '), (' D:\\train\\python\\sample\\traceback\ \sample.py ', 8, ' Bright_side_of_death ', ' Print traceback.extract_stack () ')]
The traceback of Python