Trapping Pyodbc exceptions

4,051 views
Skip to first unread message

yee...@gmail.com

unread,
Mar 2, 2009, 3:43:03 PM3/2/09
to pyodbc
Hi,

I'm having trouble trapping exceptions returned from pyodbc cursor
execute calls. Maybe I am referencing the wrong exceptions ???? I have
googled around quite a bit and it seems my code should
work...Basically, I have tried trapping all the error codes, but none
of them work. I'm connected to MS SQL database.

conn = pyodbc.connect( 'SelectWillFail * from [Test]')
cur = conn.cursor()
try:
cur.execute ( 'some garbage')
except pyodbc.Error:
print 'Error exc'
except pyodbc.DataError:
print 'Data Error'
except pyodbc.DatabaseError:
print 'Database error'
..... catch rest of the errors......
except:
print 'Why did I get here!!!!!'


I always end up with 'Why did I get here!!!!!!!'

Any ideas ?????? I MUST be doing something wrong !!!!!! Help !!!!!!

Thanks in advance for your time,
Gary

SRage

unread,
Mar 2, 2009, 4:59:17 PM3/2/09
to pyodbc
What exception are you getting?

E.g., change your last except clause to something like:

except:
print sys.exc_info()

yee...@gmail.com

unread,
Mar 2, 2009, 5:30:52 PM3/2/09
to pyodbc
Here is the sys.exc_info()..............................


(<type 'instance'>, <pyodbc.ProgrammingError instance at 0xb7d0e32c>,
<traceback object at 0xb7d0fb1c>)


So, I see pyodbc.ProgrammingError, but I can't seem to trap it.

Thanks

mkleehammer

unread,
Mar 10, 2009, 11:49:21 AM3/10/09
to pyodbc
This turns out to be related to the 'finally pops a bad exception'
issue you see discussed on other threads.

There is also an issue open for this which I will try to resolve as
soon as possible. Maybe this week. (I've been swamped for 3 months
now. There is light at the end of the tunnel, though.)
http://code.google.com/p/pyodbc/issues/detail?id=40

mkleehammer

unread,
Mar 18, 2009, 3:39:51 PM3/18/09
to pyodbc
Fixed in f98eac65ce01ccac56711293ade0c30d4c4fa39d, which will be in
2.1.5 as soon as
I release it.

Apparently *neither* solution was correct by itself -- you have to
sometimes use
in_class and sometimes the type. Python 2.6 has
PyExceptionInstance_Class exactly
for this. Since we also build for 2.4 I simply did the same thing the
macro does.
Reply all
Reply to author
Forward
0 new messages