Guide  Reference  Contents  Previous  Next  Index



Exceptions

Exceptions defined by the Objectivity for Java programming interface are contained in the com.objy.db package. Checked exceptions are derived from ObjyException. Unchecked exceptions are derived from ObjyRuntimeException.

Exceptions occur for three main reasons:

It is up to you to decide whether to catch exceptions resulting from programming errors or let the program terminate and correct the program logic.

In This Chapter

Exception Information Objects
Examples

Exception Information Objects

Exceptions thrown by Objectivity for Java may arise from errors within the Objectivity/DB kernel, or from errors detected by the programming interface. If the error originated in the Objectivity/DB kernel, the exception will have an associated vector of exception information objects; exception information objects implement the ExceptionInfo interface. If the errors were detected by the programming interface, the vector will be null.

The exception classes provide two methods relevant to exception information objects: reportErrors prints the ID and description of any kernel errors, and errors retrieves the vector of exception information objects. The order of the exception information objects in the vector is the order in which the Objectivity/DB kernel reported the errors.

Each exception information object has an error identifier, an error level, and a message describing the error. The error identifier is unique to each error, while the level number merely distinguishes between categories of error, such as fatal errors compared to warnings.

Examples

The following example catches a number of exceptions that could be thrown while scanning a container for objects that satisfy a predicate expression:

Example

This example catches any of these exceptions with a general handler for the exception superclass ObjyRuntimeException.

    session.begin();
    Iterator itr;
    try {
        itr = classAContainer.scan("ClassA", predicate);
    } catch (ObjyRuntimeException e) {
        e.printErrors();
        session.abort();
    }

This example illustrates how to retrieve and print out the properties of the exception information objects of the ObjyRuntimeException thrown by the scan method in the previous example.

    session.begin();
    Iterator itr;
    try {
        itr = classAContainer.scan("ClassA", predicate);
    } catch (ObjyRuntimeException e) {
        // Get Vector of exception information objects
        Vector errors = e.errors(); 
        // Make sure there are exception information objects
        if (errors != null) {
            // Get Enumeration from Vector
            Enumeration errs = errors.elements(); 
            ExceptionInfo ei;
                while (errs.hasMoreElements()) {
                ei = (ExceptionInfo)errs.nextElement();
                System.out.println("Id: " + ei.getId());
                System.out.println("Level: " + ei.getLevel());
                System.out.println("Message: " + ei.getMessage());
                }
        }
        session.abort();
        return;
    }
    session.commit();



Guide  Reference  Contents  Previous  Next  Index



Copyright © 2000, Objectivity, Inc. All rights reserved.