【发布时间】:2011-08-30 12:39:45
【问题描述】:
下面的sn-p:
import traceback
def a():
b()
def b():
try:
c()
except:
traceback.print_exc()
def c():
assert False
a()
产生这个输出:
Traceback (most recent call last):
File "test.py", line 8, in b
c()
File "test.py", line 13, in c
assert False
AssertionError
如果我想要完整的堆栈跟踪,包括对 a 的调用,我应该使用什么?
如果重要的话,我有 Python 2.6.6
编辑:我想获得的是相同的信息,如果我离开 try except 并让异常传播到顶层。以这个 sn-p 为例:
def a():
b()
def b():
c()
def c():
assert False
a()
产生这个输出:
Traceback (most recent call last):
File "test.py", line 10, in <module>
a()
File "test.py", line 2, in a
b()
File "test.py", line 5, in b
c()
File "test.py", line 8, in c
assert False
AssertionError
【问题讨论】:
-
我现在注意到了这个问题。我昨天answered a very similar one,这里的答案似乎很中肯。它还有一个额外的优势,它可以生成真正的回溯,您不仅可以使用
traceback函数打印,还可以将其传递给logging模块或其他地方。
标签: python exception exception-handling traceback