Enterprise Beans Exceptions Handling

  • The exceptions thrown by enterprise beans fall into two categories: system and application. If it encounters a system-level problem, your enterprise bean should throw a javax.ejb.EJBException. 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, but instead requires intervention by a system administrator.
  • 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 rollback the transaction.

Exceptions Handling Example

CartBean.java File
package com.jtechies;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import com.jtechies.exception.BookException;

 * Session Bean implementation class CartBean
// This annoatation create a stateful session bean 
public class CartBean {
	String customerName;
	String customerId;
	List<String> contents;

	public void initialize
	(String person) throws BookException {
		if (person == null) {
			throw new BookException
			("Null person not allowed.");
		} else {
			customerName = person;
		customerId = "0";
		contents = new ArrayList<String>();

	public void addBook(String title) {
	public void removeBook
	(String title) throws BookException {
		boolean result = contents.remove(title);
		if (result == false) {
			throw new BookException
			(title + " not in cart.");

	public List<String> getContents() {
		return contents;

	// container will remove the enterprise bean 
	// method completes, either normally or abnormally
	public void remove() {
		contents = null;
BookException.java File
package com.jtechies.exception;

public class BookException extends RuntimeException{
	public BookException(String exception){
ClientServlet.java File
package com.jtechies;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jtechies.CartBean;
import com.jtechies.exception.BookException;

 * Servlet implementation class ClientServlet
public class ClientServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@EJB // @EJB is used to inject EJB's
    CartBean cartBean;   
    public ClientServlet() {
        // TODO Auto-generated constructor stub
    public void init(){
    	try {
			//This will throw exception on runtime
    	catch (BookException e) {
			// TODO Auto-generated catch block
	protected void doGet(HttpServletRequest request, 
		HttpServletResponse response)
		throws ServletException, IOException{
		PrintWriter out = response.getWriter();
		Login Page</title>");
		Stateful Example!!</h2>");
		try {
			cartBean.addBook("Stephen Covey");
			cartBean.addBook("Scott Hommel");
			cartBean.addBook("Tom Risser");
			List<String> contents = 
			for(String bookName : contents)
				out.println("<h4> Book Found -> 
		catch(Exception e){

EJB Exception