Download
FAQ History |
API
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, an SQL insert fails because the database is full, or a
lookup
method cannot find the desired object. If your enterprise bean encounters a system-level problem, it should throw ajavax.ejb.EJBException
. The container will wrap theEJBException
in aRemoteException
, which it passes back to the client. Because theEJBException
is a subclass of theRuntimeException
, you do not have to specify it in thethrows
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 thedebit
business method of theSavingsAccountBean
example. Thejavax.ejb
package includes several predefined exceptions that are designed to handle common problems. For example, anejbCreate
method should throw aCreateException
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.
Table 25-1 summarizes the exceptions of the
javax.ejb
package. All of these exceptions are application exceptions, except for theNoSuchEntityException
and theEJBException
, which are system exceptions.
Download
FAQ History |
API
Search Feedback |
All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.