# 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

以上。

相关文章:

  • 2021-09-19
  • 2022-12-23
  • 2021-06-16
  • 2022-12-23
  • 2021-06-11
  • 2021-12-09
  • 2021-09-13
猜你喜欢
  • 2021-08-15
  • 2021-10-15
  • 2021-05-26
  • 2021-07-22
  • 2021-06-01
  • 2022-02-12
  • 2022-01-25
相关资源
相似解决方案