The traceback of Python

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.