This post consists the information of Exceptions in EJB perspective.We have written this post taking EJB Specifications as the reference.
Exceptions can be classified broadly into 3 categories.
- JVM Exceptions
- Application Exceptions
- System Exceptions
JVM Exceptions:These are the exceptions thrown by JVM.Nothing can be done when an JVM Exception occurs.Stop the application serverr and restart it once again.
Application Exceptions:It is nothing but a custom application defined by the application, thrown by the application.They denote that some business logic requirement is not met by the application.(Will be explained in detail with example)
System Exception:Most oftenly System exception as thrown as subclasses of the RunTime Exception by the JVM.Another type of system exception occurs when improperly configured resource is called/used.Examples of this type include using the JNDI resources with mispelt name etc.This kind of checked system exceptions must be caught and thrown as unchecked exceptions.
Thumb Rules :
1.Checked Exception is a subclass of Exception.We are forced by JVM to catch Checked Exceptions at compile time.
2.Unchecked exception is one that subclasses java.lang.RuntimeException. Subclassing java.lang.RuntimeException ensures you will not be forced by the compiler to catch the exception.
3.When we catch the exceptions don't swallow them ,throw them.
EJB3.0 Handling Exceptions:
EJB specification deals with handling two kinds of Exceptions .Application Exception and System Exception.
Application exceptions are defined in the methods of remote interfaces under the throws clause.Any special scenario in the application can be put under the Application Exception.Usually the client is given a recovery option that entails processing the request in a different way.When an application exception occurs, the EJB container doesn't roll back the transaction unless it is asked to do so explicitly, with a call to the setRollbackOnly() or it can be mentioned with an annotation in the application Exception class.
Ex:Session Bean remote Interface
@Remote
public interface TestSRemote{ public void actionShowRegn() throws HelpNotFoundException;}
Application Exception Class
@ApplicationException
public class HelpNotFoundException extends Exception {}
To throw an Application Exception
public void actionShowRegn(List lista) throws HelpNotFoundException{
if (lista.size()>=10)
{throw new HelpNotFoundException("Check the list size");}}
Next coming to System Exceptions
System exception which can be of the type Checked exception or unchecked exception.EJB methods cannot recover from these exceptions.Whenver an EJB method overcomes an unchecked exception the container will roll back the transaction.Where as in case of Checked Exceptions container does not perform the same functionality like roll backing the transaction etc.Whenever a checked system exception (such as a NamingException) occurs, you should throw javax.ejb.EJBException, or a subclass thereof, by wrapping the original exception.