Error Reporting Functions

To simplify error reporting and handling, the Db class offers several useful methods.

In addition, you can use the db_strerror() function to directly return the error string that corresponds to a particular error number.

For example, to send all error messages for a given database handle to a callback for handling, first create your callback. Do something like this:

/* 
 * Function called to handle any database error messages
 * issued by DB. 
 */
void
my_error_handler(const DbEnv *dbenv, const char *error_prefix,
	const char *msg)
{
  /* 
   * Put your code to handle the error prefix and error
   * message here. Note that one or both of these parameters
   * may be NULL depending on how the error message is issued
   * and how the DB handle is configured.
   */
} 

And then register the callback as follows:

#include <db_cxx.h>
...

Db db(NULL, 0);
std::string dbFileName("my_db.db");

try
{
    // Set up error handling for this database
    db.set_errcall(my_error_handler);
    db.set_errpfx("my_example_program"); 

And to issue an error message:

    // Open the database
    db.open(NULL, dbFileName.c_str(), NULL, DB_BTREE, DB_CREATE, 0);
}
    // Must catch both DbException and std::exception
    catch(DbException &e)
    {
        db.err(e.get_errno(), "Database open failed %s", 
            dbFileName.c_str());
        throw e;
    }
    catch(std::exception &e)
    {
        // No DB error number available, so use errx
        db.errx("Error opening database: %s", e.what());
        throw e;
    }