
# get the line number when exception occured # define a function that handles and parses psycopg2 exceptionsĮrr_type, err_obj, traceback = sys. Print ( "psycopg2 version:", psycopg2_version, " \n" ) # import the psycopg2 library's _version_ stringįrom psycopg2 import _version_ as psycopg2_versionįrom psycopg2 import connectDoes NOT need TextBroker to re-write # import the error handling libraries for psycopg2įrom psycopg2 import OperationalError, errorcodes, errors # import the connect library for psycopg2


# import sys to get more detailed Python exception info
#Code blocks an exception has been raised code
Here’s some example code showing how one can access the attribute for the PostgreSQL error code: It should be an alpha-numeric string, five characters in length, that corresponds to an exception in the PostgreSQL Error Codes table. In the psycopg2 adapter library you can return the code by accessing the exception’s pgcode attribute. There is an extensive list of over 200 error codes on the website that describes, in detail, each five-character SQL exception. InterfaceError, DatabaseError, DataError, OperationalError, IntegrityError, InternalError, ProgrammingError, and NotSupportedError. Here’s the complete list of all of psycopg2 exception classes: Complete list of the psycopg2 exception classes Some of the two most commonly occurring exceptions in the psycopg2 library are the OperationalError and ProgrammingError exception classes.Īn OperationalError typically occurs when the parameters passed to the connect() method are incorrect, or if the server runs out of memory, or if a piece of datum cannot be found, etc.Ī ProgrammingError happens when there is a syntax error in the SQL statement string passed to the psycopg2 execute() method, or if a SQL statement is executed to delete a non-existent table, or an attempt is made to create a table that already exists, and exceptions of that nature.

The except Exception as error: bit will allow you to handle any exception, and return the exception information as a TypeError class object using Python’s as keyword.Įxception libraries for the psycopg2 Python adapter Print ( "Exception TYPE:", type (error ) ) Print ( "Oops! An exception has occured:", error )
