python3的except报错怎么办

2025-12-02 0 25,940

当Python的语段用了try…except…方式之后,就一直不太知道怎么定位到详细的程序crush
位置,这两天程序需要用到这方面,于是就查了一下。

需要使用traceback包

import traceback

try:
		#以除0错误为例
		3/0
except Exception, e:
		#这个是输出错误类别的,如果捕捉的是通用错误,其实这个看不出来什么
		print 'str(Exception):t', str(Exception)       #输出  str(Exception):	
		#这个是输出错误的具体原因,这步可以不用加str,输出 
		print 'str(e):tt', str(e)   #输出 str(e):		integer division or modulo by zero
		print 'repr(e):t', repr(e) #输出 repr(e):	ZeroDivisionError('integer division or modulo by zero',)
		print 'traceback.print_exc():';    
		#以下两步都是输出错误的具体位置的
		traceback.print_exc()
		print 'traceback.format_exc():n%s' % traceback.format_exc()

另外说一下,Python 2.6之后 except那句可以换成except Exception as e了。更多学习内容,请点击python学习网。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:以上部本文内容由互联网用户自发贡献,本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。投诉邮箱:3758217903@qq.com

ZhiUp资源网 常见问题 python3的except报错怎么办 https://www.zhiup.top/8536.html

相关