Handling EJB Exceptions

This topic describes how to handle exceptions that might be thrown by the EJB that is the target of an EJB control.

To learn about EJB controls, see EJB Control: Using Enterprise Java Beans from a Web Service.

To learn about WebLogic Workshop controls, see Controls: Using Resources from a Web Service.

# EJB Exceptions Are Wrapped

If the target EJB of an EJB control throws an exception, the try-catch block must catch both the actual exception thrown and also weblogic.rmi.extensions.RemoteRuntimeException. The actual thrown exception will never be caught, but the catch block must be there for the compiler.

# Example

The following example demonstrates the technique described above.

The AccountEJB.jar defines the method withdraw(Double amount) that throws a ProcessingErrorException if the amount of the withdrawal is greater than the current balance.

        /**
         * @jws:control
         */
        private AccountEJBControl account;

       /**
         * Returns from the EJB the new account balance of the account id after a withdrawal.
         *
         * @return A String account balance, or the text of any exception that occurs.      *
         * @jws:operation
         */
        public String withdraw(String accountKey, double withdrawAmount)
        {
            try
            {
                return String.valueOf(account.findByPrimaryKey(accountKey).withdraw(withdrawAmount));
            }
            // catch the reuntime exception
            catch (weblogic.rmi.extensions.RemoteRuntimeException rre)
            {
                // find the real exception
                Throwable t = rre.getNestedException();
                if (t instanceof ProcessingErrorException)
                {
                    // perform the exception processing
                    return t.getLocalizedMessage();
                }
                else
                {
                    // re-throw any other exceptions.
                    throw rre;
                }
            }
            catch (FinderException fe)
            {
                return fe.getLocalizedMessage();
            }
            catch (RemoteException re)
            {
                return re.getLocalizedMessage();
            }
            catch (ProcessingErrorException pe)
            {
                // this keeps the compiler happy and will not execute.
                return pe.getLocalizedMessage();
            }
        }

Related Topics

Introduction to Java