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 ?

Olivier J.
  • 3,115
  • 11
  • 48
  • 71
  • possible duplicate of [Java: checked vs unchecked exception explanation](http://stackoverflow.com/questions/6115896/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. – Luiggi Mendoza Jan 10 '13 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 – Olivier J. Jan 10 '13 at 17:26
  • 1
    Read the JEE docs: [Annotation Type ApplicationException](http://docs.oracle.com/javaee/6/api/javax/ejb/ApplicationException.html). 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. – Luiggi Mendoza Jan 10 '13 at 17:31
  • So it's just because my `SimRuntimeException` is `RuntimeException` that it's encapsulated into `EJBException` ? – Olivier J. Jan 10 '13 at 17:36
  • 1
    It looks like that. It's in the [EJBException](http://docs.oracle.com/javaee/6/api/javax/ejb/EJBException.html) documentation: *The EJBException is thrown to report that the invoked business method or callback method could not be completed because of an unexpected error* – Luiggi Mendoza Jan 10 '13 at 17:45
  • Thank you for these links. I will, in my case, use Exception and `@ApplicationException(rollback=true)` because I want to gather from it informations and I don't want to "play" with `EJBException` and nested exceptions to retrieve my own. Thank you again – Olivier J. Jan 10 '13 at 17:49
  • Related / possible dupe: http://stackoverflow.com/q/32853167 – BalusC Oct 20 '15 at 19:50

1 Answers1

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.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
baumato
  • 358
  • 3
  • 13