Example 2-18 demonstrates that the traceback module allows you
Program Print abnormal trace return
(Traceback) information, similar to what the interpreter did when no exception was captured. As shown in example 2-18. 2.11.0.1. Example
2-18. Use
When we first approached Python, simple exception handling has helped us solve most of the problems, but as we go deeper, we'll find that there are a lot of situations where simple exception handling doesn't solve the problem, and the following code,
Example 2-18 shows that the Traceback module allows you to print unusual trace return (traceback) information in your program, similar to what the interpreter did when the exception was not caught. As shown in Example 2-18. 2.11.0.1. Example 2-18.
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
1. Exception stack tracking in PythonPython, in 2.x, an exception object can be any object, and the exception object and the exception stack are separate.The module for handling exception stacks in Python is the Traceback module, which provides
#!/usr/bin/env python#-*-coding:utf-8-*-#Author:love_cat#exception handling is a common occurrence in Python, where we can not only handle exceptions, but also print exceptions.Try: Print(a)#obviously a is not definedexceptException as E:Print(e)#
today DotA play well, the article comes relatively simple, hehe ~
The Traceback module is used to track exception return information. As shown in the following example:
Import traceback
try:
raise SyntaxError, "Traceback test"
except:
[Python]View plain copy 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. The following is an
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
1, if the General console program, you can use Pyerr_print (); Error messages will print directly to the console2, if not console program, you need to use Pyerr_fetch (pyobject**,pyobject**,pyobject**,pyobject**)Here is the code example: (from Stack
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.