如何获取Python线程中的异常的完整信息?

时间:2013-02-04 16:11:59

标签: python multithreading exception-handling python-2.7

当一个线程发生异常时,我得到的是:

,而不是获得包含异常行号的完整异常信息。
Unhandled exception in thread started by <function get_artist at 0x0000000002C2FEB8>

我可以手动编写try,catch程序,打印出有关异常的缺失信息吗?

我试过了:

except Exception:
    traceback.print_exc()

但是,它仍然只打印上述信息。

2 个答案:

答案 0 :(得分:1)

您是否尝试使用as获取异常?像这样......

try:
    raise Exception('MONSTER!')
except Exception as ex:
    print 'The error is a %s' % ex

>>> The error is a MONSTER!

答案 1 :(得分:0)

获取行号:

import sys

if __name__ == '__main__':
    try:
        raise Exception('MONSTER!')
    except Exception as ex:
        print sys.exc_traceback.tb_lineno