以下代码片段:
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并让异常传播到顶层时相同的信息。例如,以下代码片段:
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
traceback
函数打印它,还可以将其传递给logging
模块或其他地方。 - user4815162342