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
 */
@Stateful 
// 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) {
		contents.add(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;
	}

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

EJB Exception