【问题标题】:How to catch and display the extact error message in python如何在python中捕获并显示确切的错误消息
【发布时间】:2020-06-27 12:46:41
【问题描述】:

我有使用 python 将数据从 excel 插入 mysql 的代码。
我正在尝试捕获一些错误消息并向最终用户显示更清晰的信息,例如:

如果您尝试将 NaN 插入 mysql,系统会显示以下信息:
''' 1054(42S22):“字段列表”中的未知列“nan” '''

如何捕获此错误消息以显示更清晰的信息,例如:

if ErrorCode='1054':
    print('please check at original file at see whether there is empty value at certain area"

我用

try: 
except Exception as:
    print(e)

捕捉错误信息,但这不能显示明确的信息。
我可能错了,请随时评论。
谢谢。

【问题讨论】:

  • 您使用哪个包连接 MySQL?错误代码可能是异常的一个属性,但它可能是依赖于实现的。
  • 我使用mysql.connector。

标签: python mysql extract-error-message


【解决方案1】:

这很直接,请参阅manual

except mysql.connector.Error as err:
  if err.errno == 1054:
    print("please check at original file at see whether there is empty value at certain area")
  else:
    print("Another error")

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-22
  • 1970-01-01
  • 2020-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多