The pg_excpt class
This is the class to use for catching database errors. All pgstream code
should be enclosed into one or more try/catch blocks that catches at least
this exception.
Public functions
-
pg_excpt ()
Default Constructor. Initializes an empty exception.
-
pg_excpt (const char* query, const char* msg, std::string code="")
Constructor. query should be the SQL text where the error has been found,
or if no SQL text is available, a string pointing the context of the error.
msg is the error message itself.
code is an optional additional code that will contain the SQLSTATE
code when available. Refer to the PostgreSQL documentation,
Appendix A for available error codes
for you version (the link mentioned here is for v7.4)
-
[virtual] ~pg_excpt()
Destructor.
- std::string query() const
Returns the query (see constructor).
- std::string errmsg() const
Returns the error message (see constructor)
- std::string errcode() const
Returns the error code (sqlstate, see constructor)
- std::string full_error_txt() const
Returns a multi-line error text containing the query, the error message
and the error code when available.
Example of use
The following example shows how to catch a violation of unique constraint
in order to silently ignore it, while still processing other errors:
try {
pg_stream s2("INSERT INTO mytable(pk) VALUES(:val)", cnx);
s << val;
}
catch (pg_excpt p) {
if (p.errcode()!="23505") { // UNIQUE VIOLATION?
std::cerr << p.full_error_txt(); // alert the user only if it is something else
}
}