python – traceback.format_exc / print_exc在期待回溯时返回None

前端之家收集整理的这篇文章主要介绍了python – traceback.format_exc / print_exc在期待回溯时返回None前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我无法弄清楚为什么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 
最佳答案
尝试在my_excepthook中更改为:

print "".join(traceback.format_exception(type,tb))
原文链接:https://www.f2er.com/python/439266.html

猜你在找的Python相关文章