Search code examples
javajakarta-eeexceptionjava-ee-6ejb-3.1

EJB3.1 System Exceptions vs javax.ejb.EJBException


Just a bit of background on EJB3.1 exceptions before bringing up my question -

Application exceptions comprise of

  • User defined checked or unchecked exceptions which have @ApplicationException annotation

  • All checked exceptions

    java.lang.Exception & it's subclass exceptions
    except java.rmi.RemoteException & it's subclass exceptions

System exceptions comprise of

  • java.rmi.RemoteException and it's subclass exceptions

  • All unchecked exceptions

    java.lang.RuntimeException and it's subclass exceptions
    java.lang.Error and it's subclass exceptions

Below is a statement I have read in this book

In EJB System exceptions are not excepted by the client, when encountered such exceptions are not passed to the client as is but are wrapaped in a javax.ejb.EJBException.

My questions -

  1. Are all application exceptions described above supposed to be thrown directly by EJB to client?
  2. If System Exceptions are wrapped inside javax.ejb.EJBException before throwing to the client, then is javax.ejb.EJBException considered as System Exception?

Solution

    1. Yes, more or less that's how they work. For further details on EJB's behaviors check out the following blog post: Link

    Also from this question:

    An application exception shall be thrown when there is a business-logic error, as opposed to a system error.

    There is an important difference: application exceptions do not automatically cause a transaction to rollback. The client has an opportunity to recover after an application exception is thrown.

    Application exceptions are sent to the client without being repackaged as an EJBException. Therefore, you can use them to report validation errors or business logic problems, and they will reach the client.

    1. javax.ejb.EJBException extends RuntimeException so yes, it is a System Exception.

    Common scenario associated with this: if you've got an uncaught RuntimeException in your application code it will roll-back. It's pretty useful default behavior.