【发布时间】:2012-02-09 00:59:33
【问题描述】:
环境 - win7 机器上的 Python 2.7.2。 我的技能水平 - 菜鸟
我正在使用以下内容来捕获和打印异常堆栈跟踪 -
def logerr(stmt, e):
try:
##do something
except:
print '##EXCEPTION in logging: '
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(exc_type, exc_value, exc_traceback, file=sys.stdout)
输出是 -
##EXCEPTION in logging:
Traceback (most recent call last):
File "C:\Users\amurty\Desktop\dev\eclipse\workspace\hhs\FeedSearch\src\main\main.py", line 18, in main
log()
TypeError: log() takes exactly 1 argument (0 given)
我想缩进堆栈跟踪。所以输出应该是这样的 -
##EXCEPTION in logging:
Traceback (most recent call last):
File "C:\Users\amurty\Desktop\dev\eclipse\workspace\hhs\FeedSearch\src\main\main.py", line 18, in main
log()
TypeError: log() takes exactly 1 argument (0 given)
我怎样才能做到这一点。 pprint 或 textwrap 模块在这里有帮助吗?
【问题讨论】:
标签: python exception exception-handling python-2.7