Exception catching

Since all methods can now throw sidl.RuntimeException , Babel ensures there is an out argument to hold an exception. If not explicitly specified, Babel will automatically add the argument. For maximum backward compatibility, the exception argument type is sidl.BaseInterface, while the base exception class is sidl.SIDLException. The exception argument, which behaves like an out parameter, will appear after the return value when both occur in a method. After the call, the client should test this argument using is_null or not_null. If it is not_null_null, an exception was thrown by the method so the caller should respond appropriately. When an exception is thrown, the values of all other arguments are undefined. So the best course of action is to ignore them. If the code does not check the exception argument after each call (that can throw one), any exceptions that are thrown will be utterly ignored as a result of not being automatically propagated to higher level routines.

It is possible to determine which exception was thrown through casting the argument. A successful cast indicates the type of exception that occurred. An example of this process is illustrated below. Package ExceptionTest has a class named Fib with a getFib method declared in SIDL as follows$:$


int getFib(in int n, in int max_depth, in int max_value, in int depth)
    throws NegativeValueException, FibException;

The code to catch specified exception types is$:$


  use sidl
  use ExceptionTest_Fib
  use ExceptionTest_FibException
  use ExceptionTest_NegativeValueException
  use sidl_BaseInterface
  type(ExceptionTest_Fib_t)                    :: fib
  type(sidl_BaseInterface_t)                   :: except, except2
  type(ExceptionTest_FibException_t)           :: fibexcept
  type(ExceptionTest_NegativeValueException_t) :: nvexcept
  integer (kind=sidl_int)  :: index, maxdepth, maxval, depth, result
  call new(fib, except)

  index    = 4
  maxdepth = 100
  maxvalue = 32000
  depth    = 0
  call getFib(fib, index, maxdepth, maxvalue, depth, result, except)
  if (not_null(except)) then
    call cast(except, fibexcept, except2)
    if (not_null(fibexcept)) then
!      do something here with the FibException
       call deleteRef(fibexcept, except2)
    else
      call cast(except, nvexcept, except2)
!     do something here with the NegativeValueException
      call deleteRef(nvexcept, except2)
    endif
    call deleteRef(except, except2)
  else
    write (*,*) 'getFib for ', index, ' returned ', result
  endif
  call deleteRef(fib, except2)

NOTE$:$ Any caller of a method that returns an exception should ignore the values of out and inout parameters. Anything not freed becomes a reference and memory leak.



babel-1.4.0
users_guide Last Modified 2008-10-16

http://www.llnl.gov/CASC/components
components@llnl.gov