python显示哪一行发生错误,以及什么错误


# coding: utf-8
# author: lovecjy
# created_time: 2020/9/27
"""
使用try...except...代码异常时,显示哪一行发生错误,以及什么错误
"""
import logging
try:
	print(a)
except Exception as e:
	# 错误的行和错误     error
	error_line = e.__traceback__.tb_lineno
	error_info = '第{error_line}行发生error为: {e}'.format(error_line=error_line, e=str(e))
	logging.error(error_info)

# 运行代码,结果:
# ERROR:root:第9行发生error为: name 'a' is not defined

以上。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM