473,385 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Cannot catch _mysql_exceptions.OperationalError

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
Jan 18 '08 #1
2 10984
Bob wrote:
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
Jan 18 '08 #2
Bob wrote:
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:
------------------------------------------------
>>import MySQLdb
try:
.... db = MySQLdb.connect(user='jeffrey', host='localhost')
.... except MySQLdb.OperationalError:
.... print 'caught'
....
caught
>>import _mysql_exceptions
try:
.... db = MySQLdb.connect(user='jeffrey', host='localhost')
.... except _mysql_exceptions.OperationalError:
.... print 'caught'
....
caught
>>from _mysql_exceptions import OperationalError
try:
.... db = MySQLdb.connect(user='jeffrey', host='localhost')
.... except OperationalError:
.... print 'caught'
....
caught
>>MySQLdb.OperationalError is _mysql_exceptions.OperationalError
True
>>MySQLdb.__version__
'1.2.2'
------------------------------------------------
Jeffrey
Jan 18 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: F. GEIGER | last post by:
I've troubles to let my app take off using pysqlite. What I wonder most for now is that "pysqlite2.dbapi2.OperationalError: cannot commit transaction - SQL statements in progress" when I do this:...
0
by: Scupper | last post by:
I am using the System.Management namespace for the first time and I cannot get the ManagementObject.SetPropertyValue method to work properly. I am trying to change the ServerName and ShareName...
6
by: Aaron Smith | last post by:
Ok. I have a dataset that has multiple tables in it. In one of the child tables, I have a column that I added to the DataSet (Not in the DataSource). This column does not need to be stored in the...
3
by: thomson | last post by:
Hi All, Are there any sort of exceptions that cannot be caught by the Runtime, Any Insights? Thanks in Advance thomson
29
by: dbhbarton | last post by:
Had a thought that's grown on me. No idea if it's original or not- too inexperienced in programming- but I guess there's no harm floating it out there. Python wins big on readability, and...
2
by: Filipe Sousa | last post by:
Hi I'm trying to use sqlite with python 2.5 but I have this problem: Traceback (most recent call last): File "converter.py", line 13, in <module> c_old.execute('select id_aluno, nome from...
6
by: Dave | last post by:
I really don't like the users getting an unhandled expception page, and I'm still to new with ASP.Net and C#. So please accept my appology for the 2 part question. SqlException (0x80131904) ...
2
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double)...
2
by: Hexade | last post by:
Hello I would like to use the safe "?" placeholder in my SQLite requests but I got the following error: Traceback (most recent call last): (...) cursor.execute("SELECT ? FROM ? WHERE name =...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.