![图片[1]-python3的except报错怎么办-uusu优素-乐高,模型,3d打印,编程](http://uusucn.zbbe.cn/wp-content/uploads/2024/01/5daea1ca4a463681.gif)
当Python的语段用了try…except…方式之后,就一直不太知道怎么定位到详细的程序crush
位置,这两天程序需要用到这方面,于是就查了一下。
需要使用traceback包
importtraceback
try:
#以除0错误为例
3/0
exceptException,e:
#这个是输出错误类别的,如果捕捉的是通用错误,其实这个看不出来什么
print'str(Exception):\t',str(Exception)#输出str(Exception):	<type'exceptions.Exception'>
#这个是输出错误的具体原因,这步可以不用加str,输出
print'str(e):\t\t',str(e)#输出str(e):		integerdivisionormodulobyzero
print'repr(e):\t',repr(e)#输出repr(e):	ZeroDivisionError('integerdivisionormodulobyzero',)
print'traceback.print_exc():';
#以下两步都是输出错误的具体位置的
traceback.print_exc()
print'traceback.format_exc():\n%s'%traceback.format_exc()原文来自:https://www.py.cn                    © 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
    



















































 
        

暂无评论内容