Exception Handling in EJBs

Discussions

EJB programming & troubleshooting: Exception Handling in EJBs

  1. Exception Handling in EJBs (3 messages)

    Hi,

    Being new to EJB development i am facing some problems i am sure some of you will be able to help me.
    The configurations are
    Application Server: IPlanet Application Server 6.0 SP3

    We have a user defined exception called UserException which derives from RemoteException one of the method(innerMethod) which is inside aEJB but not a remote method(remoteMethod) throws this UserException our remoteMethod throws only RemoteException no other exception
    code is something like

    public class MyEJB {

    Public remoteMethod() throws RemoteException {

    innerMethod();//call inner method

    }

    Public innerMethod() throws UserException {

    //some code which may throw UserException
    }
    }

    public class UserException extends RemoteException
    {
    ///some code
    public int getErrorCode()
    {}
    }


    public class MyEJBCallingClass {

    public void myEJBCallingMethod()
    {
    try{
    //call ejbs

    }catch(remoteException re){

    }
    catch(UserException ue){
    //check for the error code

    }

    }



    We have coded innerMethod so that it puts a code inside the UserException before throwing this Exception this error code is being checked by myEJBCallingMethod to find out which is the error that has happened in the EJB.
    But what is happening is that when the UserException is thrown by the innerMethod the myEJBCallingMethod gets ServerException and not the UserException so the error code can't be checked as it's the method of UserException but we get ServerException and the ServerException encaptulates the stack trace for UserException.
    The problem is that the error code can't be checked by the myEJBCallingMethod as it dosen't gets UserException

    Do anybody know whats wrong with the above code ?
    Why UserException is not thrown from the MyEJB ?
    What's the right way for throwing User defined exception from the EJBs.

    any help will be appreciated.
    -Ashish.

    Threaded Messages (3)

  2. Exception Handling in EJBs[ Go to top ]

    UserException shouldn't extend RemoteException (treat that type as "special."

    Rather, UserException should extend Exception (in this case anyhow).

    Youre remoteMethod() should declare that it throws UserException. All the EJB spec mandates is that your remote methods must throw RemoteException. It doesn't mandate they must ONLY throw RemoteException.

    HTH

    Chz

    Tony
  3. Exception Handling in EJBs[ Go to top ]

    Hi Ashish,

    "A ServerException is thrown as a result of a remote method call if the execution of the remote method on the server machine throws a RemoteException."

    This means that if a RemoteException or its sub-class is thrown from the server, rmi-runtime will wrap this exception in ServerException and will marshall the ServerException on the client side. This is how RMI marshalls RemoteException and its sub-classes. So an the client side you will receive ServerException in such cases.

    There is field *detail* inside the ServerException class that actually represents the RemoteException thrown on the client side. In *myEJBCallingMethod()* in *MyEJBCallingClass* you can catch this ServerException and from the field *detail* you can perform error-code checking.

    Regards,
    Hemant
    ServerTeam,
    www.pramati.com

  4. Exception Handling in EJBs[ Go to top ]

    Thanks For the help i have done this and this works.