views:

265

answers:

1

In a try/except block, how do I extract the Oracle error number?

+5  A: 
try:
   cursor.execute("select 1 / 0 from dual")
except cx_Oracle.DatabaseError, exc:
   error, = exc
   print "Code:", error.code
   print "Message:", error.message

This results in the following output:

Code: 1476
Message: ORA-01476: divisor is equal to zero
Mark Harrison