C++ exceptions
Prev | Chapter 17. Upgrading Berkeley DB 4.0 applications to Berkeley DB 4.1 | Next

## C++ exceptions

With default flags, the C++ DbEnv and Db classes can throw exceptions from their constructors. For example, this can happen if invalid parameters are passed in or the underlying C structures could not be created. If the objects are created in an environment that is not configured for exceptions (that is, the DB_CXX_NO_EXCEPTIONS flag is specified), errors from the constructor will be returned when the handle's open method is called.

In addition, the behavior of the DbEnv and Db destructors has changed to simplify exception handling in applications. The destructors will now close the handle if the handle's close method was not called prior to the object being destroyed. The return value of the call is discarded, and no exceptions will be thrown. Applications should call the close method in normal situations so any errors while closing can be handled by the application.

This change allows applications to be structured as follows:

    try {
    	DbEnv env(0);
    	env.open(/* ... */);
    	Db db(&env, 0);
    	db.open(/* ... */);
    	/* ... */
    	db.close(0);
    	env.close(0);
    } catch (DbException &dbe) {
    	// Handle the exception, the handles have already been closed.
    }

Prev | Up | Next
Java exceptions | Home | Application-specific logging and recovery