Question

I am confused by a vague point in the definition and the possible impact of Application Exceptions and System Exceptions in EJB. In particular I cannot figure out if the Exception defined as:

public class SomeException extends Exception{
...
}

is going to be an Application or System Exception. According to the specification an Application Exception should always be defined with the annotation @ApplicationException. I infer hence that the above class is not an Application Exception even though it is a checked Exception. My questions coming out pertain to the correct Java Syntax and subsequently to the behaviour of the EJB Container in case this Exception is to be thrown within a business method of a Session Bean (not Singleton though):

@Stateless
@LocalBean
public class StatelessBean{

public void doStuff(){
throw new SomeException(); 
}

}

1) Should the SomeException be declared in the throw clause?

2) Does the Container discards the instance after the method doStuff() is called?

3) If it is finally a Sytem Exception is my assumption correct, that a System Exception could be a checked and not RuntimeException?

Update:

To my opinion it is not definitely clear according to the spec that a checked Exception should always be an Application Exception. What if one would define a checked RuntimeException? Should it be included in throw of a method? Is it a System or Application Exception?

 public class SomeException extends RuntimeException{
    ...
    }
Was it helpful?

Solution

The spec says:

Application exceptions that are checked exceptions may be defined as such by being listed in the throws clauses of the methods of the bean’s business interface, no-interface view, home interface, component interface, and web service endpoint. An appli- cation exception that is an unchecked exception is defined as an application exception by annotating it with the ApplicationException metadata annotation, or denoting it in the deployment descriptor with the application-exception element.

So, the above class is an Application exception, since it's a checked exception. And of course it must be listed in the throws clause of your method, otherwise the code won't compile. And no, the bean instance won't be discarded after this exception is thrown, as per the spec.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top