[Enterprise Extensions only]

CORBA support, handling exceptions

The preferred coding practice for handling errors in C++ and Java is by using exceptions, which is supported by using the standard try and throw logic of exception handling. Handling exceptions is a critical part of the client programming model. The exceptions that are thrown must be understood and handled appropriately by application developers.

In some cases, a server implementation object can encounter an error for which it might need to throw an exception to the client to give the client the opportunity to recover from the error.

This topic provides the following information about handling exceptions:

CORBA support, using appropriate exceptions

CORBA exceptions are used to communicate between server implementation objects and client applications. You must follow specific rules regarding which CORBA exceptions to use. The following abstract CORBA exception classes are defined:

CORBA::Exception
This is the abstract class that is the base of all CORBA exceptions. Because this class is abstract, it is never thrown. However, it can be used in catch blocks to process all CORBA exceptions in one block.
CORBA::UserException
This is the abstract class for all CORBA user exceptions and is a subclass of CORBA::Exception. This class should be used as the base class of all user-defined exception classes. The contents of these classes have no special format. Methods that throw these classes must declare their usage in IDL using the raises clause.
CORBA::SystemException
This is the abstract class for all CORBA standard exceptions and is a subclass of CORBA::Exception. These exceptions can be thrown by any method regardless of the interface specification. Standard exceptions cannot be listed in raises expressions, therefore whether or not an interface throws a system exception is unknown. This means that you should be prepared to handle standard exceptions on all method calls. Each standard exception includes a minor code to provide more detailed information.

Any method can throw a standard exception, even if there are no exceptions declared in the raises clause of that method, so a method can throw an exception at any time.

Note: CORBA standard exceptions are a predefined list of exceptions that can be thrown from any method. CORBA has defined the class that provides this support as CORBA::System Exception. For more information about CORBA exceptions, see The Common Object Request Broker: Architecture and Specification.

CORBA support, catching exceptions

Client programs are required to handle exceptions, because the default behavior for uncaught exceptions is to end the process. (If the client process ends unexpectedly, suspect an uncaught exception.)

A client program can handle exceptions within the catch clause of a try/catch block that encompasses remote method invocations or calls to ORB services. Typically exception instances are actually instances of either the SystemException or UserException classes.

When deciding how or what exceptions to catch in a client application, consider the following general rules for exception handling:

Specific standard exceptions cannot be caught individually. If you need to handle individual standard exceptions, you can do so within a CORBA::SystemException catch block and use the .id() method.

Consider the following simple client example:

try
{
   // Some real code goes here
   foo.boo();
}
// Catch and process specific User exceptions
...
// Catch any other User exceptions defined for the method in the
// `raises' clause
catch (CORBA::UserException &ue)
{
   // Process any other User exceptions. Use the .id() method to
   // record or display useful information
   cout << "Caught a User Exception: " << ue.id() << endl;
}
// Catch any System exceptions
catch (CORBA::SystemException &se)
{
   // Process any System exceptions. Use the .id(), and .minor()
   // methods to record or display useful information
   cout << "Caught a System Exception: " << ue.id() << ": " << 
        ue.minor() << endl;
}
catch (...)
{
   // Process any other exceptions. This would catch any other C++
   // exceptions and should probably never occur
   cout << "Caught an unknown Exception" << endl;
}