As discussed in Chapter 4 in the section entitled "SOM Interface Definition Language," a method may be declared to return zero or more exceptions. IDL exceptions are implemented by simply passing back error information after a method call, as opposed to the "catch/throw" model where an exception is implemented by a long jump or signal. Associated with each type of exception is a name, and optionally, a struct-like data structure for holding error information. A method declares the types of exceptions it may return in a raises expression.
Below is an example IDL declaration of a "BAD_FLAG" exception, which may be "raised" by a"checkFlag" method, as part of a "MyObject" interface:
interface MyObject { exception BAD_FLAG { long ErrCode; char Reason[80];} void checkFlag(in unsigned long flag) raises(BAD_FLAG); };
An exception structure contains whatever information is necessary to help the caller understand the nature of the error. The exception declaration can be treated like a struct definition: i.e., whatever you can access in an IDL struct, you can access in an exception declaration. Alternatively, the structure can be empty, whereby the exception is just identified by its name.
The SOM Compiler will map the exception declaration in the above example to the following C language constructs:
typedef struct BAD_FLAG { long ErrCode; char Reason[80]; } BAD_FLAG; #define ex_BAD_FLAG "MyObject::BAD_FLAG"
When an exception is detected, the "checkFlag" method must call SOMMalloc to allocate a "BAD_FLAG" structure, initialize it with the appropriate error information, and make a call to somSetException (see "Setting an exception value," below) to record the exception value in the Environment structure passed in the method call. The caller, after invoking "checkFlag", can check the Environment structure that was passed to the method to see if there was an exception, and if so, extract the exception value from the Environment (see "Getting an exception value," below.)