# 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
以上。