我知道print(e)
(其中e是异常)打印发生的异常,但是,我正在尝试寻找与Java的e.printStackTrace()
等效的python,它可以准确地跟踪异常发生的行并打印出它的整个跟踪。
谁能告诉我Python中e.printStackTrace()
的等价物是什么?
发布于 2012-03-04 21:15:19
import traceback
traceback.print_exc()
当在except ...:
块中执行此操作时,它将自动使用当前异常。有关详细信息,请参阅http://docs.python.org/library/traceback.html。
发布于 2013-05-07 19:19:40
还有logging.exception
。
import logging
...
try:
g()
except Exception as ex:
logging.exception("Something awful happened!")
# will print this message followed by traceback
输出:
ERROR 2007-09-18 23:30:19,913 error 1294 Something awful happened!
Traceback (most recent call last):
File "b.py", line 22, in f
g()
File "b.py", line 14, in g
1/0
ZeroDivisionError: integer division or modulo by zero
(来自http://blog.tplus1.com/index.php/2007/09/28/the-python-logging-module-is-much-better-than-print-statements/通过How to print the full traceback without halting the program?)
https://stackoverflow.com/questions/9555133
复制相似问题