[英]traceback.format_exc/print_exc returns None when expecting traceback
我無法弄清楚為什么traceback.format_exc()在以下示例中返回“None”:
#!/usr/bin/env python
import sys
import traceback
def my_excepthook(type, value, tb):
print type.__name__
print value
# the problem: why does this return "None"?
print traceback.format_exc(tb) # see http://docs.python.org/library/traceback.html#traceback.format_exc
sys.excepthook = my_excepthook # see http://docs.python.org/library/sys.html#sys.excepthook
# some code to generate a naturalistic exception
a = "text"
b = 5
error = a + b
使用Python 2.7.1,我得到以下輸出:
TypeError
cannot concatenate 'str' and 'int' objects
None
而不是第3行的“無”,我希望得到當我注釋掉sys.excepthook行時會發生什么:
Traceback (most recent call last):
File "log-test.py", line 17, in <module>
error = a+b
嘗試在my_excepthook中更改為:
print "".join(traceback.format_exception(type, value, tb))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.