title: JSF 2 Global exception handling, navigation to error page not happening
body: <p>I am developing a JSF 2.0 based web application. I am trying to implement a global exception handler which will redirect the user to a generic error page whenever any exception occurs (e.g. NullPointerException,ServletException,ViewExpiredException etc.)</p>
  
  <p>Whenever a NPE occurs in my app, My customnavhandler breakpoint is hit and NavigationHandler code is executed, but somehow redirection to error page is not happening, the requested page remains partially rendered. Any idea what could be wrong here ? One info is that I am throwing an NPE deliberately on the requested page (which was partiallyu rendered after NPE)</p>
  
  <p>My faces-config.xml entry</p>
  
  <pre><code>&lt;factory&gt;
    &lt;exception-handler-factory&gt;
      com.common.exceptions.CustomExceptionHandlerFactory
    &lt;/exception-handler-factory&gt;
  &lt;/factory&gt;
  </code></pre>
  
  <p>My CustomNavHandler  </p>
  
  <pre><code>public class CustomExceptionHandler extends ExceptionHandlerWrapper {
  
  private static final Logger logger = Logger.getLogger("com.gbdreports.common.exception.CustomExceptionHandler");
  private final ExceptionHandler wrapped;
  
  public CustomExceptionHandler(ExceptionHandler wrapped) {
      this.wrapped = wrapped;
  }
  
  @Override
  public ExceptionHandler getWrapped() {
      return this.wrapped;
  
  }
  public void handle() throws FacesException {
      final Iterator&lt;ExceptionQueuedEvent&gt; i = getUnhandledExceptionQueuedEvents().iterator();         
  
      while (i.hasNext()) {             
          ExceptionQueuedEvent event = i.next();             
          ExceptionQueuedEventContext context =                    
                  (ExceptionQueuedEventContext) event.getSource();               
          // get the exception from context             
          Throwable t = context.getException();               
          final FacesContext fc = FacesContext.getCurrentInstance();   
          final ExternalContext externalContext = fc.getExternalContext();
          final Map&lt;String, Object&gt; requestMap = fc.getExternalContext().getRequestMap();            
          final ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler) fc.getApplication().getNavigationHandler();               
          //here you do what ever you want with exception             
          try {                   
              //log error ?      
              logger.error("Severe Exception Occured");
              //log.log(Level.SEVERE, "Critical Exception!", t);                   
              //redirect error page                 
              requestMap.put("exceptionMessage", t.getMessage());                 
              nav.performNavigation("/TestPRoject/error.xhtml");                 
              fc.renderResponse();                   
              // remove the comment below if you want to report the error in a jsf error message                 
              //JsfUtil.addErrorMessage(t.getMessage());               
              } 
          finally {                 
              //remove it from queue                 
              i.remove();             }         
          }         
      //parent hanle         
      getWrapped().handle(); 
          }
  
  }
  </code></pre>
  
  <p>My customNavhandler factory</p>
  
  <pre><code>public class CustomExceptionHandlerFactory extends ExceptionHandlerFactory {
  
  
   private ExceptionHandlerFactory parent;
  
    public CustomExceptionHandlerFactory(ExceptionHandlerFactory parent) {
      this.parent = parent;
    }
  
    @Override
    public ExceptionHandler getExceptionHandler() {
        return new CustomExceptionHandler (parent.getExceptionHandler());
  
    }
  
  }
  </code></pre>