python捕捉详细异常堆栈的方法

python中有 try——except 的方法捕获异常,可以获取到异常的种类以及自定义异常,

但是有时候对于debug测试来说,信息不全,比如说 触发异常的具体位置在哪:

import traceback
try:
    num= int('abc')
except Exception:
   traceback.print_exc()

traceback.print_exc() 直接打印异常
traceback.format_exc()返回字符串
还可以将信息写入到文件
traceback.print_exc(file=open(‘error.txt’,’a+’))

猜你喜欢

转载自www.cnblogs.com/klsw/p/11426938.html