我想捕获并记录异常而不退出,例如,
try:
do_stuff()
except Exception as err:
print(Exception, err)
# I want to print the entire traceback here,
# not just the exception name and details
我想打印与引发异常时打印的输出完全相同的输出,而没有 try/ except 拦截异常,我这样做not希望它退出我的程序。
traceback.format_exc() http://docs.python.org/3/library/traceback.html#traceback.format_exc or sys.exc_info() http://docs.python.org/3/library/sys.html#sys.exc_info如果您想要的话,将产生更多信息。
import traceback
import sys
try:
do_stuff()
except Exception:
print(traceback.format_exc())
# or
print(sys.exc_info()[2])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)