Cannot catch _mysql_exceptions.OperationalError

Bob rswerdlow at goombah.com
Fri Jan 18 02:17:02 CET 2008


In our database code (we are using django v0.96) we wanted to catch
and handle MySQL OperationalErrors.  We use both the Django models and
database connections.

However, even though we confirmed that a
_mysql_exceptions.OperationalError are raised (particularly 1213
deadlock), we cannot catch them with try/except.

Here's the code that did not work:

    import _mysql_exceptions
    from _mysql_exceptions import OperationalError

    try:
        database_code()
    except (_mysql_exceptions.OperationalError, OperationalError), e:
        error_handling()

Originally, we just had one import, but tried both to ensure that was
not the problem.  In debugging, we confirmed that both type(e) and
e.__class_ were <class _mysql_exceptions.OperationalError>.

The only work-around we found was:

    try:
        database_code()
    except Exception, e:
        if e.__class__.__name__.find('OperationalError') != -1:
            error_handling()
        else:
            raise

This seems silly.  If we used any other exception, the except clause
seems to work correctly.

Is there a problem with _mysql_exceptions.OperationalError?  Is there
a better work-around?

Thanks,
Bob





More information about the Python-list mailing list