1-32 SOMobjects Base Toolkit: Programmer’s Reference Manual
C Example
/* IDL declaration of class X: */
interface X : SOMObject {
exception OUCH {long code1; long code2; };
void foo(in long arg) raises (OUCH);
};
/* implementation of foo method */
SOM_Scope void SOMLINK foo(X somSelf, Environment *ev, long arg)
{
X_OUCH *exception_params; /* X_OUCH struct is defined
in X’s usage bindings
*/
if (arg > 5) /* then this is a very bad error */
{
exception_params = (X_OUCH*)SOM_Malloc(sizeof(X_OUCH));
exception_params–>code1 = arg;
exception_params–>code2 = arg–5;
somSetException(ev, USER_EXCEPTION, ex_X_OUCH,
exception_params);
/* the Environment ev now contains an X_OUCH exception,
with
* the specified exception_params struct. The constant
* ex_X_OUCH is defined in foo.h. Note that
exception_params
* must be malloced.
*/
return;
}
...
}
main()
{
Environment *ev;
X x;
somEnvironmentNew();
x = Xnew();
ev = somGetGlobalEnvironment();
X_foo(x, ev, 23);
if (ev–>_major != NO_EXCEPTION) {
printf(”foo exception = %s\n”, somExceptionId(ev));
printf(”code1 = %d\n”,
((X_OUCH*)somExceptionValue(ev))–>code1);
/* finished handling exception. */
/* free the copied id and the original X_OUCH structure:
*/
somExceptionFree(ev);
}
...
}
Related Information
Functions: somExceptionId, somExceptionValue, somExceptionFree,
somGetGlobalEnvironment
Data Structures: Environment, exception_type, string (somcorba.h)
Komentarze do niniejszej Instrukcji