What's the purpose of throwing an EJBException ?

Discussions

EJB design: What's the purpose of throwing an EJBException ?

  1. Hello,
    I'd like to hear somebody's else opinion about EJBException,
    I can see that it's an exception descendant of RuntimeException,
    so what is the puropose of throwing it? - I sometimes see it thrown in the method signature of EJB methods or at the end of a method...is it poor coding or it has some utility ? (Sorry I cannot imagine a parallel with ordinary Java programming...I never needed to throw for example a Null Pointer Exception in my code...that's why I wonder about this question)
    Regards
    Francesco
  2. Maybe this is one explanation:

    An EJB exception is a System exception. When ever a System exception occurs in EJB the EJB container automatically handles it. The conatiner will:

    1. Roll back the transaction
    2. Log the exception to alert the system admin
    3. Discard the bean instance
    4. Throw a Remote Exception or one of its subtypes

    To do all this you may need to explicity throw the EJB Exception.

    Samir

  3. You need to throw this exception explictly for informing the container that there is a system excetpion (For ex: SQLException for database unavailability etc..)

    However, you do not need to declare the exception in the throws clause of the method signature, since its a sub-class of runtime exception.
  4. Actually, I believe that ejb 1.1 containers will handle any runtime exception in the same manner as EJBException (as outlined in previous message). I'm working on a project that needs to execute in weblogic 5.1 and websphere 1.0. I've coded a new exception class, EJBExceptionMigrate, that extends RuntimeException. I throw this exception in the same manner as EJBException (wrapping jndi,sql exceptions,etc.), but this allows the EJB 1.0 websphere environment to handle the exception correctly as well. This is detailed in ibm's redpiece on weblogic to websphere migration.