Elegant Python-use traceback to get detailed exception information

Source: Internet
Author: User
Tags integer division
Try:
    1/0
except exception,e:
    print E


The output is an integer division or modulo by zero, only to know that the error is reported, but do not know in which file which function which line of the error. Use the Traceback module below

Import traceback
try:
    1/0
except exception,e:
    Traceback.print_exc ()

The output result is traceback (most recent call last): File "test_traceback.py", line 3, in <module> 1/0 Zerodivisionerror:intege R division or modulo by zero is very intuitive and facilitates debugging.
What is the difference between traceback.print_exc () and Traceback.format_exc ()? Format_exc () returns the string, Print_exc () is printed directly. That is, Traceback.print_exc () is the same as the print traceback.format_exc () effect. The Print_exc () can also accept the file parameter to be written directly to one of the files. For example, Traceback.print_exc (File=open (' tb.txt ', ' w+ ')) is written to the Tb.txt file.

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.