An IDL exception maps to a C++ class that derives from the standard UserException
class which is defined in the CORBA module and which contains the exception's data. The derived class is like a variable-length struct and each exception member must handle its own storage. The copy constructor, assignment operator, and destructor automatically copy or free the storage associated with the exception.
The UserException
class is derived from CORBA::Exception
, which is defined in the CORBA namespace:
//C++ class UserException : public Exception { . . . };
The Exception
base class can only be instantiated as part of an instance of a derived class.
The following exception in IDL:
//IDL exception Discard { string logic; short x; }
This example maps to the following C++ code:
//C++ class Discard : public CORBA::UserException { public: CORBA::String_mgr logic; CORBA::Short x; Discard(const char* _logic, const CORBA::Short& _x); Discard( ); static Discard* _narrow(CORBA::Exception *e); }
The C++ mapping defines a constructor that has one parameter for each exception member. The constructor initializes the exception members to the specified values. String exception types require a const char*
parameter.
In the example, the constructor has two parameters, Logic
and x
, one for each field in the exception. The exception can be thrown as follows:>
// C++ throw Discard("Explanation", 10)
The exception classes support the _narrow( )
static member function. The function's parameter is a pointer to an object of class CORBA::Exception
. It is the base class of all user-defined and system exceptions. You can call CORBA::Exception
as follows:
//IDL interface Customer exception Reject { . . . }; Account newCustomer( ) raises (Reject); } // C++ Exception* e = . . . // Narrow e so it is a Customer::Reject exception if (Customer::Reject* r = Customer::Reject::narrow(e)) else . . .
The default constructor performs no member initialization. The copy constructor, assignment operator, and destructor automatically copy or free the storage related to the exception.