以下片段:
import traceback
def a():
b()
def b():
try:
c()
except:
traceback.print_exc()
def c():
assert False
a()
Run Code Online (Sandbox Code Playgroud)
生成此输出:
Traceback (most recent call last):
File "test.py", line 8, in b
c()
File "test.py", line 13, in c
assert False
AssertionError
Run Code Online (Sandbox Code Playgroud)
如果我想要完整的堆栈跟踪包括对一个?的调用,我应该使用什么?
如果重要的话我有Python 2.6.6
编辑:我想得到的是,如果我离开尝试除外,我将得到的相同信息,并将异常传播到顶层.这个代码段例如:
def a():
b()
def b():
c()
def c():
assert False
a()
Run Code Online (Sandbox Code Playgroud)
生成此输出:
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 …Run Code Online (Sandbox Code Playgroud) 在除了块之外的python中,我想打印错误消息,但我不希望程序停止执行,我明白我必须做这样的事情
try:
1/0
except:
print errorMessage
Run Code Online (Sandbox Code Playgroud)
在除了部分之外,我期待像java这样的东西 printStackTrace()