Use Traceback to track exceptions in Python

Source: Internet
Author: User

test1.py, when the denominator is 0, the calling system exits

#!/usr/bin/pythonimport Sysdef Division (a=1, b=1):    if b==0:         print ' b eq 0 '         sys.exit (1)    else:         Return A/b

test2.py, use try: Except catches the exception, and then Traceback.print_exc () prints.

#!/usr/bin/pythonimport sysimport tracebackimport test1  a=10b=0 try:    print test1.division (A, B) except:    print ' invoking division failed. '    Traceback.print_exc ()    sys.exit (1)

Run test2.py failed to throw an exception.

$python test2.py
Execution Python-2.5.1/python (enodeb/linux)
b eq 0
Invoking division failed.
Traceback (most recent):
File "test2.py", line ten, in <module>
Test1.division (A, B)
File "/home/fesu/test1.py", line 6, in Division
Sys.exit (1)
Systemexit:1


Use Traceback to track exceptions in 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.