Cannot catch _mysql_exceptions.OperationalError

B

Bob

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
 
J

John Nagle

Bob said:
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.

If you're using MySQLdb, it works fine:

try:
... do database operations
except MySQLdb.OperationalError, message: # handle trouble
errorcode = message[0] # get MySQL error code
if errorcode == kmysqlduplicateentry : # if dup on insert
... deal with duplicate entry

If Django has a problem, you'll have to take that up with them.

John Nagle
 
J

Jeffrey Froman

Bob said:
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()

Both of the above forms work fine here, as does using
MySQLdb.OperationalError:
------------------------------------------------.... db = MySQLdb.connect(user='jeffrey', host='localhost')
.... except MySQLdb.OperationalError:
.... print 'caught'
....
caught
.... db = MySQLdb.connect(user='jeffrey', host='localhost')
.... except _mysql_exceptions.OperationalError:
.... print 'caught'
....
caught
.... db = MySQLdb.connect(user='jeffrey', host='localhost')
.... except OperationalError:
.... print 'caught'
....
caught
'1.2.2'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top