Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
41 lines (31 sloc) 1.24 KB

Exception Handling

All exceptions raised by cx_Oracle are inherited from :attr:`cx_Oracle.Error`. See :ref:`Exceptions <exceptions>` for more details on the various exceptions defined by cx_Oracle. See the exception handling section in the :ref:`API manual <exchandling>` for more details on the information available when an exception is raised.

Applications can catch exceptions as needed. For example, when trying to add a customer that already exists in the database, the following could could be used to catch the exception:

try:
    cursor.execute("insert into customer values (101, 'Customer A')")
except cx_Oracle.IntegrityError:
    print("Customer ID already exists")
else:
    print("Customer added")

If information about the exception needs to be processed instead, the following code can be used:

try:
    cursor.execute("insert into customer values (101, 'Customer A')")
except cx_Oracle.IntegrityError as e:
    errorObj, = e.args
    print("Customer ID already exists")
    print("Error Code:", errorObj.code)
    print("Error Message:", errorObj.message)
else:
    print("Customer added")
You can’t perform that action at this time.