24.Catching Exceptions
All exceptions raised by python-oracledb are inherited fromoracledb.Error
. SeeOracledb Exceptions andOracledb _Error Objects forinformation about attributes.
SeeError Handling in Thin and Thick Modes for differences between the python-oracledb Thin andThick modes.
Applications can catch exceptions as needed. For example, when trying to add acustomer that already exists in the database, the following could be used tocatch the exception:
try:cursor.execute("insert into customer values (101, 'Customer A')")exceptoracledb.IntegrityError:print("Customer ID already exists")else:print("Customer added")
If information about the exception needs to be processed instead, the followingcode can be used:
try:cursor.execute("insert into customer values (101, 'Customer A')")exceptoracledb.IntegrityErrorase:error_obj,=e.argsprint("Customer ID already exists")print("Error Code:",error_obj.code)print("Error Full Code:",error_obj.full_code)print("Error Message:",error_obj.message)else:print("Customer added")
This will print output like:
CustomerIDalreadyexistsErrorCode:1ErrorFullCode:ORA-00001ErrorMessage:ORA-00001:uniqueconstraint(CJ.PK)violated
24.1.Error Handling in Thin and Thick Modes
The Thin and Thick modes of python-oracledb return some errors differently.
The python-oracledb Thin mode code generates error messages with the prefix“DPY”.
In python-oracledbThick mode:
The Oracle Call Interface (OCI) libraries generate error messages with theprefix “ORA”.
TheODPI-C code layer generates errormessages with the prefix “DPI”.
The python-oracledb Thick mode code generates error messages with the prefix“DPY”.
Errors generated by the Oracle Database itself commonly have the error prefix“ORA”.
Some differences between python-oracledb Thin and Thick mode errors are shownin the examples below:
Binding: When binding is incorrect, the python-oracledb Thick mode maygenerate an Oracle Client library error such as:
ORA-01008:notallvariablesbound
In contrast, the python-oracledb Thin mode might generate:
DPY-4010:abindvariablereplacementvalueforplaceholder":1"wasnotprovided
Connection messages: The python-oracledb Thin mode connection and networkingis handled by Python itself. Some errors portable across operating systemsand Python versions have DPY-prefixed errors displayed by python-oracledb.Other messages are returned directly from Python and may vary accordingly.The traditional Oracle connection errors with prefix “ORA” are not shown. Forexample, the scenarios detailed below show how the connection and networkerror messages might differ between the python-oracledb Thin and Thick modes.
Scenario 1: The given host does not have a database listener running.
python-oracledb Thin mode Error:
DPY-6005:cannotconnecttodatabase.Connectionfailedwith"[Errno 61]Connectionrefused"
python-oracledb Thick mode Error:
ORA-12541:TNS:nolistener
Scenario 2: The requested connection alias was not found in the tnsnames.ora file.
python-oracledb Thin mode Error:
DPY-4000:cannotconnecttodatabase.Unabletofind"{name}"in{file_name}
python-oracledb Thick mode Error:
ORA-12154:TNS:couldnotresolvetheconnectidentifierspecified
Scenario 3: The Oracle Database listener does not know of the requestedservice name.
python-oracledb Thin mode Error:
DPY-6001:cannotconnecttodatabase.Service"{service_name}"isnotregisteredwiththelistenerathost"{host}"port{port}.(SimilartoORA-12514)
python-oracledb Thick mode Error:
ORA-12514:TNS:listenerdoesnotcurrentlyknowofservicerequestedinconnectdescriptor
Connection Pooling: The python-oracledb Thin mode pool is not based on theOracle Call Interface (OCI) Session Pool and has its own DPY messages.