Example

Let us define an IDL interface for a "MyObject" object, which declares a "BAD_FLAG" exception, which can be raised by the "checkFlag"method, in a file called "myobject.idl":

   interface MyObject {
     exception BAD_FLAG { long ErrCode;   char Reason[80]; }

     void checkFlag(in unsigned long flag) raises(BAD_FLAG);
   };

The SOM IDL compiler will map the exception to the following C language constructs, in myobject.h:

   typedef struct BAD_FLAG {
      long ErrCode;
      char Reason[80];
   } BAD_FLAG;

   #define ex_BAD_FLAG "MyObject::BAD_FLAG"

A client program that invokes the "checkFlag" method might contain the following error handling code. (Note: The error checking code below lies in the user-written procedure, "ErrorCheck," so the code need not be replicated through the program.)

   #include "som.h"
   #include "myobject.h"
   boolean ErrorCheck(Environment *ev);   /* prototype */

   main()
   {
       unsigned long flag;
       Environment ev;
       MyObject myobj;
       char     *exId;
       BAD_FLAG *badFlag;
       StExcep  *stExValue;

       myobj = MyObjectNew();
       flag  = 0x01L;
       SOM_InitEnvironment(&ev);

        /* invoke the checkFlag method, passing the Environment param */
       _checkFlag(myobj, &ev, flag);

       /* check for exception */
       if (ErrorCheck(&ev))
       {
          /* ... */
          somExceptionFree(&ev);   /* free the exception memory */
       }

       /* ... */
   }

   /* error checking procedure */
   boolean ErrorCheck(Environment *ev)
   {
     switch (ev._major)
     {
     case SYSTEM_EXCEPTION:
       /* get system exception id and value */
       exId      =  somExceptionId(ev);
       stExValue =  somExceptionValue(ev);
       /* ... */
       return( TRUE);

     case USER_EXCEPTION:
       /* get user-defined exception id and value */
       exId =  somExceptionId(ev);
       if (strcmp(exId, ex_BAD_FLAG) == 0)
       {
          badFlag = (BAD_FLAG *)  somExceptionValue(ev);
          /* ... */
       }
       /* ... */
       return( TRUE);

     case NO_EXCEPTION:
       return( FALSE);
     }
   }

The implementation of the "checkFlag" method might contain the following error-handling code:

   #include "som.h"
   #include "myobject.h"

   void  checkFlag(MyObject somSelf, Environment *ev,
                   unsigned long flag)
   {
      BAD_FLAG *badFlag;
      /* ... */

      if ( /* flag is invalid */ )
      {
         badFlag = (BAD_FLAG *) SOMMalloc(sizeof(BAD_FLAG));
         badFlag->ErrCode = /* bad flag code */;
         strcpy(badFlag->Reason, "bad flag was passed");
         somSetException(ev, USER_EXCEPTION,
                         ex_BAD_FLAG, (void *)badFlag);

         return;
      }
      /* ... */
   }


[Back: Getting an exception value]
[Next: Memory management]