我使用的是mysql.connector模块。如果connection.commit()抛出了一个错误,即"do the operation“命令,我如何确定这个错误的类是否是它的”类包“的成员(如果这是正确的话)?
所以我有:
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的一个实例进行线路测试。我以一种特殊的方式处理它,因为在我的上下文中它不是一个“致命的”错误。
但另一个错误刚刚出现,这是另一个非致命的错误,也需要以一种特殊的方式处理,即mysql.connector.errors.IntegrityError。
我想捕获/过滤"package of classes“mysql.connector.errors中的所有错误。我该怎么做?
发布于 2020-09-01 01:08:39
使用:
if issubclass(e, connector.errors.Error):
# do error handling for mysql related errors
else:
# do error handling for other errors发布于 2020-09-01 01:10:30
您可以尝试在错误是上述类的实例的任何地方使用isinstance()。
例如
try:
conn.commit()
except Exception as e:
if isinstance(e, mysql.connector.errors.ErrorName):
#do somethinghttps://stackoverflow.com/questions/63675119
复制相似问题