tags:

views:

42

answers:

1

I throw NullPointerException in a java bean and catch the exception in FacesServletWrapper. in FacesServletWrapper I gets always only ServletException.

  1. how can I catch the specific exception that I throw?

  2. How can I continue from where I throws the exception?

in my bean:

 public String getSize() {  
  try {
   Object object = null;
   object.equals("");

  } catch (Exception e) {
   throw new NullPointerException();
  }


 }

my servlet:

 public class FacesServletWrapper extends MyFacesServlet {

 public static final String CONFIG_FILES_ATTR = "javax.faces.CONFIG_FILES";
 public static final String LIFECYCLE_ID_ATTR = "javax.faces.LIFECYCLE_ID";

 private ServletConfig servletConfig;
 private FacesContextFactory facesContextFactory;
 private Lifecycle lifecycle;

 @Override
 public void service(ServletRequest request, ServletResponse response) throws IOException, ServletException {
  FacesContext facesContext = facesContextFactory.getFacesContext(servletConfig.getServletContext(), request, response, (javax.faces.lifecycle.Lifecycle) lifecycle);
  try {
   super.service(request, response);
  } catch (Throwable e) {
   Locale locale = (Locale) facesContext.getExternalContext().getSessionMap().get(Constants.LOCALE);
   ServletContext context = servletConfig.getServletContext();
   RequestDispatcher dispatcher = context.getRequestDispatcher("/errors/error.jsf");

   if (e instanceof NullPointerException) {
                        //here I catch  only ServletException
    String error = ResourceUtil.getMessage("Login_failed", locale);
    facesContext.getExternalContext().getSessionMap().put("error", error);
    dispatcher.forward(request, response);
    ((HttpServletResponse) response).sendRedirect(((HttpServletRequest) request).getContextPath() + "/errors/error.jsf");
   } 
  }

 }

 public void destroy() {
  servletConfig = null;
  facesContextFactory = null;
  lifecycle = null;
 }

 public ServletConfig getServletConfig() {
  return servletConfig;
 }

 private String getLifecycleId() {
  String lifecycleId = servletConfig.getServletContext().getInitParameter(LIFECYCLE_ID_ATTR);
  return lifecycleId != null ? lifecycleId : LifecycleFactory.DEFAULT_LIFECYCLE;
 }

 @Override
 public void init(ServletConfig servletConfig) throws ServletException {
  super.init(servletConfig);
  this.servletConfig = servletConfig;
  facesContextFactory = (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
  LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
  lifecycle = (Lifecycle) lifecycleFactory.getLifecycle(getLifecycleId());
 }
}

Thanks!

A: 

You're calling FacesServlet#service() here:

try {
    super.service(request, response);
} catch (Throwable e) {
    // ...
}

Here's an extract from its javadoc to learn what kind of exception it may throw:

If a FacesException is thrown in either case, extract the cause from the FacesException. If the cause is null extract the message from the FacesException, put it inside of a new ServletException instance, and pass the FacesException instance as the root cause, then rethrow the ServletException instance. If the cause is an instance of ServletException, rethrow the cause. If the cause is an instance of IOException, rethrow the cause. Otherwise, create a new ServletException instance, passing the message from the cause, as the first argument, and the cause itself as the second argument.

In other words, it will always throw either ServletException or IOException. You need to use Throwable#getCause() to extract the desired cause from the catched ServletException and then determine it further. E.g.

if (e.getCause() instanceof NullPointerException) {
    // ...
}
BalusC
Hi!I realized that i had to go through 4 e.getCause() like this:e.getCause().getCause().getCause().getCause().I will probably need to loop until i get a null result and the previous one will be my exception.Do you have an answer regarding my second question - How can I continue from where I throws the exception? I mean, I would like to catch the exception and return to the user's code in the bean as if it's a regular try{}catch{} in the bean?
Rivki