[英]How do I get the stack trace from an Exception Object in Python 2.7?
How can I get the full stack trace from the Exception object itself?如何从异常 object 本身获取完整的堆栈跟踪?
Consider the following code as reduced example of the problem:考虑以下代码作为问题的简化示例:
last_exception = None
try:
raise Exception('foo failed')
except Exception as e:
print "Exception Stack Trace %s" % e
The stack trace itself is not stored in the exception object itself.堆栈跟踪本身不存储在异常 object 本身中。 However, you can print the stack trace of the last recent exception using sys.exc_info()
and the traceback
module.但是,您可以使用sys.exc_info()
和traceback
模块打印最近一次异常的堆栈跟踪。 Example:例子:
import sys
import traceback
try:
raise Exception('foo failed')
except Exception as e:
traceback.print_tb(*sys.exc_info())
If you do not want to display the stack trace immediately, it should be possible to store the return value of sys.exc_info()
somewhere.如果您不想立即显示堆栈跟踪,应该可以将sys.exc_info()
的返回值存储在某处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.