Python/Django: how do I determine the class to specify in an "except" statement?

Viewed 205

I am working with some Python/Django code that I inherited. In it there is a DB call surrounded by a try block, with an "except Exception ex" statement to catch all exceptions. I want to be more selective, and catch only the exception type that I anticipate. By examining the Exception object that is caught, I can tell that it's of type "DatabaseError".

The comments in my code below show the many things I have tried, based on Googling the question and searching here, along with the errors that Python gives me when I try them. Most frustrating is that I've found plenty of examples on the web of code the people say works, that looks just like what I'm trying. But the code samples I find generally don't include the "import" lines.

I suspect I need to import something more, but I can't figure out what.

import cx_Oracle
## import cx_Oracle.DatabaseError # creates an error saying "No name 'DatabaseError in module 'cx_Oracle'"
## from cx_Oracle import DatabaseError # creates an error saying "No name 'DatabaseError in module 'cx_Oracle'"
. . .
    connection = connections['ims_db']

    sqlUpdateQuery = "SELECT * FROM LOCKING WHERE IDENT={0} AND KEY_ID='SL_KEY' FOR UPDATE NOWAIT".format(self.serviceUid)
    cursor = connection.cursor()
    try:
        cursor.execute(sqlUpdateQuery)

    # this gets the error "Undefined variable 'Database'"
    ## except Database.DatabaseError as dbe3:

    # this gets the error "Undefined variable 'Oracle'"
    ## except Oracle.DatabaseError as dbe2:

    # this gets the error "Module 'cx_Oracle' has no 'DatabaseError' member"
    ## except cx_Oracle.DatabaseError as dbe:

    # this gets the error "Undefined variable 'DatabaseError'"
    ## except DatabaseError as dbe:

    # this gets the error "Catching an exception which doesn't inherit from BaseException: cx_Oracle"
    ## except cx_Oracle as dbe:

    # this gets the error "Module cx_Oracle has no '_error' member"
    ## except cx_Oracle._Error as dbe:

    except Exception as ex:
        # This gets the error "Module cx_Oracle has no 'DatabaseError' member"
        ## if isinstance(ex, cx_Oracle.DatabaseError) :

        # This gets the error "Undefined variable 'DatabaseError'"
        ## if isinstance(ex, DatabaseError) :

        className = type(ex).__name__
        # This logs "... class = DatabaseError ..."
        log.error("Exception (class = {1}) while locking service {0}".format(self.serviceUid, className))
        args = ex.args
        arg0=args[0]
        # arg0ClassName is "_Error"
        arg0ClassName = type(arg0).__name__
        code = arg0.code
        # codeClassName is "int"
        codeClassName = type(code).__name__
        msg = "Exception, code = {0}".format(code)
        log.debug(msg)
        raise
1 Answers

The correct syntax is as follows:

try:
    cur.execute("some_sql_statement")
except cx_Oracle.DatabaseError as e:
    error, = e.args
    print("CONTEXT:", error.context)
    print("MESSAGE:", error.message)

You can see that syntax in a few of the samples (like TypeHandlers.py) that you can find here: https://github.com/oracle/python-cx_Oracle/tree/master/samples.

Try running the samples and working with them to see if you can resolve your issue. If not, please create an issue containing a complete runnable sample here: https://github.com/oracle/python-cx_Oracle/issues.

Related