The J2EETM Tutorial
Home
TOC
PREV TOC NEXT Search
Feedback

Handling Exceptions

The exceptions thrown by enterprise beans fall into two categories: system and application.

A system exception indicates a problem with the services that support an application. Examples of these problems include the following: a database connection cannot be obtained, a SQL insert fails because the database is full, a lookup method cannot find the desired object. If your enterprise bean encounters a sytem-level problem, it should throw a javax.ejb.EJBException. The container will wrap the EJBException in a RemoteException, which it passes back to the client. Because the EJBException is a subclass of the RuntimeException, you do not have to specify it in the throws clause of the method declaration. If a system exception is thrown, the EJB container might destroy the bean instance. Therefore, a system exception cannot be handled by the bean's client program; it requires intervention by a system administrator.

An application exception signals an error in the business logic of an enterprise bean. There are two types of application exceptions: customized and predefined. A customized exception is one that you've coded yourself, such as the InsufficentBalanceException thrown by the debit business method of the AccountEJB example. The javax.ejb package includes several predefined exceptions that are designed to handle common problems. For example, an ejbCreate method should throw a CreateException to indicate an invalid input parameter. When an enterprise bean throws an application exception, the container does not wrap it in another exception. The client should be able to handle any application exception it receives.

If a system exception occurs within a transaction, the EJB container rolls back the transaction. However, if an application exception is thrown within a transaction, the container does not roll back the transaction.

The following table summarizes the exceptions of the javax.ejb package. All of these exceptions are application exceptions, except for the NoSuchEntityException and the EJBException, which are system exceptions.

Table 8 Exceptions 
Method Name
Exception It Throws
Reason for Throwing
ejbCreate
CreateException
An input parameter is invalid.
ejbFindByPrimaryKey
(and other finder methods that return a single object)
ObjectNotFoundException
(subclass of FinderException)
The database row for the requested entity bean is cannot be found.
ejbRemove
RemoveException
The entity bean's row cannot be deleted from the database.
ejbLoad
NoSuchEntityException
The database row to be loaded cannot be found.
ejbStore
NoSuchEntityException
The database row to be updated cannot be found.
(all methods)
EJBException
A system problem has been encountered.

Home
TOC
PREV TOC NEXT Search
Feedback