Python Driver: Error Handling

Effective error handling is crucial for developing robust applications that interact with databases. The Polypheny Python Driver incorporates a comprehensive error handling mechanism that aligns with the Python Database API Specification v2.0 (PEP 249). This section outlines the common types of errors you might encounter while using the Polypheny Python Driver and how to handle them appropriately. Understanding Exceptions

Exceptions

The Polypheny Python Driver defines several exceptions that inherit from the Python Exception class. These exceptions help in identifying and handling different error conditions that may arise during database operations. The primary exceptions include:

  • Error: This is the base exception class for all other error exceptions. All exceptions provided by this driver inherit from this class.

  • InterfaceError: Raised for errors related to the database interface rather than the database itself, such as a failed connection or a problem with the configuration of the connection.

  • DatabaseError: This is a generic error class for all errors related to the database. It serves as the base class for more specific database error exceptions.

  • DataError: Indicated problems with the processed data like division by zero, numeric value out of range, etc.

  • OperationalError: Related to the database’s operational aspects, such as unexpected disconnections, transaction failures, or issues with memory allocation.

  • IntegrityError: Indicates errors related to the relational integrity of the database, such as foreign key violations.

  • InternalError: Used for errors related to the database’s internal operations, such as a cursor no longer valid, or issues related to the internal configuration of the database.

  • ProgrammingError: Raised for programming errors, such as table not found, syntax errors in the SQL statement, wrong number of parameters specified, etc.

  • NotSupportedError: This exception is raised in cases where an attempted operation is not supported by the database, such as calling a rollback on a database that does not support transaction or attempting to use a feature that is not implemented.

Handling Exceptions

When writing code that interacts with the Polypheny database, it is recommended to wrap database operations in try-except blocks to gracefully handle exceptions and maintain the stability of your application. Here’s an example of how to handle exceptions:

from polypheny import Connection
from polypheny.exceptions import ProgrammingError, OperationalError

try:
    conn = Connection(address='localhost', port=20590, username='user', password='password', transport='http')
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM non_existing_table")
except ProgrammingError as e:
    print(f"Programming error occurred: {e}")
except OperationalError as e:
    print(f"Operational error occurred: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")
finally:
    cursor.close()
    conn.close()

In this example, different types of exceptions are caught and handled separately, allowing for specific error messages or recovery actions depending on the error type.

© Polypheny GmbH. All Rights Reserved.