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