gpt4 book ai didi

python - 检查一个类是否是包的成员

转载 作者:行者123 更新时间:2023-12-03 08:39:11 24 4
gpt4 key购买 nike

我正在使用 mysql.connector 模块。如果由于 connection.commit() 的结果而引发错误,即“执行操作”命令,我如何确定该错误的类是否是其“类包”的成员(如果这是正确的词)?
所以我有:

try:
conn.commit()
except Exception as e:
# this line works fine:
# if type( e ) == mysql.connector.errors.DataError:
# this doesn't:
if type( e ) in mysql.connector.errors:
# do some error-handling, logging, etc.
首先,我对 e 的线路测试很满意。是 mysql.connector.errors.DataError 的一个实例.我以一种特殊的方式处理它,因为在我的上下文中它不是一个“致命”错误。
但是刚刚出现另一个错误,这是另一个非 fatal error ,也需要以特殊方式处理, mysql.connector.errors.IntegrityError .
我想捕获/过滤“类包”中的所有错误 mysql.connector.errors .我怎么做?

最佳答案

采用:

if issubclass(e, connector.errors.Error):
# do error handling for mysql related errors
else:
# do error handling for other errors

关于python - 检查一个类是否是包的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63675119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com