Exception Handling
Exception handling is particularly relevant to this discussion because, unlike in container-managed persistence, in bean-managed persistence the bean developer is responsible for throwing the correct exceptions at the right moments. For this reason, we’ll take a moment to discuss the different types of exceptions in BMP. This discussion will be useful when we get into the details of database access and implementing the callback methods.
Bean-managed beans throw three types of exceptions:
- Application exceptions
Application exceptions include standard EJB application exceptions and custom application exceptions. The standard EJB application exceptions are
CreateException,FinderException,ObjectNotFoundException,DuplicateKeyException, andRemoveException. These exceptions are thrown from the appropriate methods to indicate that a business logic error has occurred. Custom exceptions are exceptions developed for specific business problems. We cover developing custom exceptions in Chapter 11.- Runtime exceptions
Runtime exceptions are thrown from the virtual machine itself and indicate that a fairly serious programming error has occurred. Examples include the
NullPointerExceptionandIndexOutOfBoundsException. These exceptions are handled by the container automatically and should not be handled inside a bean method.You will notice that all the callback methods (
ejbLoad( ),ejbStore( ),ejbActivate( ),ejbPassivate( ), andejbRemove( )) throw anEJBExceptionwhen a ...