Recall Subsection 10.2.3 discussed issues associated
with SIDL support for exception interfaces versus Java's requirement
that all exceptions be classes. Below is an extension of the example
in Subsection 10.3.7
involving getFibi, which could throw one of the following two
interface exceptions NegativeValueException and
TooDeepException.
public int getFibi_Impl ( /*in*/ int n) throws NegativeValueException.Wrapper, TooDeepException.Wrapper { // DO-NOT-DELETE splicer.begin(ExceptionTest.Fib.getFibi) if (n < 0) { FibException fex = new FibException(); NegativeValueException.Wrapper neg = (NegativeValueException.Wrapper) NegativeValueException.Wrapper._cast(fex); neg.setNote("n negative"); throw neg; } // .... Do Fibonacci stuff .... // DO-NOT-DELETE splicer.end(ExceptionTest.Fib.getFibi) }
Notice that the interface exceptions and their Wrappers cannot be instantiated directly. Instead, a FibException object is created then cast to the appropriate exception interface type. As in Subsection 10.3.7, the wrapper class's full name is required during the cast operation. Finally, the example illustrates the use of setNote to add the message to the exception being thrown -- which is necessary since the note cannot be passed to the exception's constructor.