Chapter 2. Exception Handling and Debugging

Table of Contents

Debugging BDB XML Applications

Before continuing, it is helpful to look at exception handling and debugging tools in the BDB XML API.

All BDB XML operations can throw an exception, and so they should be within a try block.

BDB XML methods throw XmlException objects. BDB XML always re-throws all underlying Berkeley DB exceptions as XmlException, so every exception that can be thrown by BDB XML is an XmlException instance.

XmlException is derived from std::exception, so you are only required to catch std::exception in order to provide proper exception handling for your BDB XML applications. Of course, you can choose to catch both types of exceptions if you want to differentiate between the two in your error handling or messaging code.

Note that if you are using core Berkeley DB operations with your BDB XML application then you should catch either DbException or std::exception with this code.

The following example illustrates BDB XML exception handling.

Example 2.1 BDB XML Exception Handling

#include "DbXml.hpp"

using namespace DbXml;
int main(void)
{
    // Open an XmlManager and an XmlContainer.
    XmlManager myManager;
    try {
        XmlContainer myContainer = 
            myManager.openContainer("container.dbxml");
    } catch (XmlException &xe) {
        // Error handling goes here
    } catch (std::exception &e) {
        // Error handling goes here
    }
} 

Note that, you can obtain more information on the cause of the XmlException by examining the underlying error code. Do this using the XmlException::getExceptionCode() method. See the Berkeley DB XML C++ API reference for details on the exception codes available through this class.

Debugging BDB XML Applications

In some cases, the exceptions thrown by your BDB XML application may not contain enough information to allow you to debug the source of an error. In this case, you can cause BDB XML to issue more information using the error stream.

In order to set up the error stream, you use set_error_stream() on the underlying Berkeley DB environment object (see Berkeley DB Environments for information on environments):

Example 2.2 Setting Error Streams

#include "DbXml.hpp"

using namespace DbXml;

int main(void)
{
    // Open an XmlManager
    XmlManager myManager;
    myManager.getDbEnv()->set_error_stream(std::cerr);
} 

Once you have set up your error stream, you can control the amount of information that BDB XML reports on that stream using setLogLevel() and setLogCategory.

setLogLevel() allows you to indicate the level of logging that you want to see (debug, info, warning, error, or all of these).

setLogCategory() allows you to indicate the portions of DB XML's subsystems for which you want logging messages issued (indexer, query processor, optimizer, dictionary, container, or all of these).

You can call these from anywhere within your DB XML code. For example:

Example 2.3 Setting Log Levels

#include "DbXml.hpp"

using namespace DbXml;
int main(void)
{
    // Open an XmlManager and an XmlContainer.
    XmlManager myManager;
    db.getDbEnv()->set_error_stream(std::cerr);
    try {
        XmlContainer myContainer = db.openContainer("container.dbxml");
        DbXml::setLogLevel(DbXml::LEVEL_ALL, true);
        DbXml::setLogCategory(DbXml::CATEGORY_ALL, true); 
    } catch (XmlException &xe) {
        // Error handling goes here
    } catch (std::exception &e) {
        // Error handling goes here
    }
}