5

I call account EJB method in JSF bean like that :

try{
   account.someFunction(...);
}catch(SimRuntimeException e){
   logger.log(Level.FATAL, "SimRuntimeException catched !");
}catch(SimNotRuntimeException e){
   logger.log(Level.FATAL, "SimNotRuntimeException catched !");
}catch(Exception e){
   logger.log(Level.FATAL, "Exception catched !");
}

My Exceptions :

public class SimRuntimeException extends RuntimeException {

   public SimRuntimeException() {
      super();
   }

}



@ApplicationException(rollback=true)
public class SimNotRuntimeException extends Exception {

   public SimNotRuntimeException() {
      super();  
   }


}
  • when account.someFunction(...); throws SimRuntimeException I fall into Exception block because my SimRuntimeException is wrapped into EJBException probably by EJB container.
  • when account.someFunction(...); throws SimNotRuntimeException I fall as expected into SimNotRuntimeException

So, what is concretely the difference between Exception with @ApplicationException(rollback=true) and RuntimeException please ?

7
  • possible duplicate of Java: checked vs unchecked exception explanation. Your problem is about understanding the difference between Exception and RuntimeException, and that question contains a well explained answer. Commented Jan 10, 2013 at 17:21
  • That's not the case. I'm wondering what is the difference between Exception WITH @ApplicationException(rollback=true) to automatcally rollback transaction and RuntimeException which will rollback transaction too but will encapsulate this exception into EJBException Commented Jan 10, 2013 at 17:26
  • 1
    Read the JEE docs: Annotation Type ApplicationException. The main difference is in the checked/unchecked exception, the rollback=true is just a property if you want to rollback any open transaction in case this exception is raised. Commented Jan 10, 2013 at 17:31
  • So it's just because my SimRuntimeException is RuntimeException that it's encapsulated into EJBException ? Commented Jan 10, 2013 at 17:36
  • 1
    It looks like that. It's in the EJBException documentation: The EJBException is thrown to report that the invoked business method or callback method could not be completed because of an unexpected error Commented Jan 10, 2013 at 17:45

1 Answer 1

7

Pal's blog states:

EJB makes a difference in Application Exceptions and System Exceptions. Application exception is something that you define, you throw, and you are aware of. By default the application exception does not cause a rollback, unless you define it that way (and I think it's recommended). Every checked exception that is mentioned in the method signature and also any checked or unchecked exception that is annotated with @ApplicationException, is an application exception.

System exceptions happen in cases, you don't control, and they are unchecked exceptions. They always cause rollback. Good practice is, if you wrap checked exceptions -- that cannot be avoided -- in your method into EJBException e.g. ParseException.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.