无法在Python中捕获MySQL IntegrityError

我使用Python / Bottle / SqlAlchemy / MySQL作为Web服务.

我试图通过调用存储过程来捕获IntegrityError,但我无法做到这一点.

用这个

cursor = connection.cursor()
cursor.callproc('my_stored_proc',[arguments])

产生与…相同的结果

try:
    cursor = connection.cursor()
    cursor.callproc('my_stored_proc',[arguments])
except IntegrityError as e:
    print("Error: {}".format(e))
    return {"message": e.message}

在这两种情况下我都会遇到IntegrityError异常.为什么在后一种情况下没有发现异常?
最佳答案
问题是我抓到了不正确的异常.

事实证明,引发的错误实际上是pymysql.err.IntegrityError类型而不是我假设的sqlalchemy.exc.IntegrityError.

我通过这样做找到了异常类型:

import sys
try:
    cursor = connection.cursor()
    cursor.callproc('my_stored_proc',[arguments])
except:
    print "Unexpected error:",sys.exc_info()[0]

我看到了这个打印输出:

Unexpected error:

dawei

【声明】:丽水站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

相关文章

【免责声明】本站内容转载自互联网,其发布内容言论不代表本站观点,如果其链接、内容的侵犯您的权益,烦请提交相关链接至邮箱pniu8212@foxmail.com我们将及时予以处理。

Copygight © 2016-2023 https://www.0578zz.cn All Rights Reserved.丽水站长网

站长:pniu8212#foxmail.com(请把#换成@)