[英]How do I get the stack trace from an Exception Object in Python?
How can I get the full stack trace from the Exception object itself? 如何从Exception对象本身获取完整的堆栈跟踪?
Consider the following code as reduced example of the problem: 考虑以下代码作为问题的简化示例:
last_exception = None
try:
raise Exception('foo failed')
except Exception as e:
last_exception = e
# this happens somewhere else, decoupled from the original raise
print_exception_stack_trace(last_exception)
Edit: I lied, sorry. 编辑:我撒了谎,对不起。 e.__traceback__
is what you want. e.__traceback__
是您想要的。
try:
raise ValueError
except ValueError as e:
print( e.__traceback__ )
>c:/python31/pythonw -u "test.py"
<traceback object at 0x00C964B8>
>Exit code: 0
This is only valid in Python 3; 这仅在Python 3中有效 。 you can't do it in earlier versions. 您无法在早期版本中做到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.