sql_exception

Detailed Description

Exception class for SQL related errors.

Thrown for API and SQL errors. Inherits from std::runtime_error.

The error_code member provides the native error code from the underlying database, enabling programmatic error handling such as retry logic for deadlocks or special handling for constraint violations. Error codes are database-specific:

  • PostgreSQL: SQLSTATE codes encoded as integers (use constants from SQLState.h)
  • MySQL/MariaDB: Native error codes from mysql_errno() (include <mysqld_error.h>)
  • SQLite: Extended error codes (include <sqlite3.h>)
  • Oracle: ORA- error numbers (numeric portion only)

Example (PostgreSQL):

try {
con.execute(sql);
} catch (const zdb::sql_exception& e) {
// Handle duplicate key
// Handle FK constraint failure
// Retry transaction
} else {
std::cerr << "Error (" << e.error_code << "): " << e.what() << std::endl;
}
}
#define SQLSTATE_foreign_key_violation
Definition SQLState.h:251
#define SQLSTATE_unique_violation
Definition SQLState.h:252
#define SQLSTATE_deadlock_detected
Definition SQLState.h:331
Exception class for SQL related errors.
Definition zdbpp.h:293
const int error_code
Native error code from the underlying database.
Definition zdbpp.h:301

For MySQL, SQLite, and Oracle, include the respective database header with error codes or use numeric codes directly.

Public Member Functions

 sql_exception (const char *msg="SQLException", int code=0)
 Constructs a new sql_exception.

Constructor & Destructor Documentation

◆ sql_exception()

sql_exception ( const char * msg = "SQLException",
int code = 0 )
explicit

Constructs a new sql_exception.

Parameters
msgError message (defaults to "SQLException")
codeDatabase-specific error code (defaults to 0)

Member Data Documentation

◆ error_code

const int error_code

Native error code from the underlying database.

The value is 0 if no specific error code was provided. Consult your database's documentation for code meanings.

Copyright © Tildeslash Ltd. All rights reserved.