Custom JSF Exception Handler

This Exception Handler will log any exception wrapped by JSF lifecycle.

Extends ExceptionHandlerWrapper

package com.origami.censo.jsf;

import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.FacesException;
import javax.faces.application.NavigationHandler;
import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerWrapper;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;
import javax.faces.event.ExceptionQueuedEvent;
import javax.faces.event.ExceptionQueuedEventContext;

public class CustomExceptionHandler extends ExceptionHandlerWrapper {
    private ExceptionHandler wrapped;
    private static final Logger LOG = Logger.getLogger(CustomExceptionHandler.class.getName());
    public CustomExceptionHandler(ExceptionHandler wrapped) {
      this.wrapped = wrapped;
  public ExceptionHandler getWrapped() {
    return wrapped;

  public void handle() throws FacesException {
    Iterator iterator = getUnhandledExceptionQueuedEvents().iterator();
    while (iterator.hasNext()) {
      ExceptionQueuedEvent event = (ExceptionQueuedEvent);
      ExceptionQueuedEventContext context = (ExceptionQueuedEventContext)event.getSource();
      Throwable throwable = context.getException();
      FacesContext fc = FacesContext.getCurrentInstance();
      try {
//          Flash flash = fc.getExternalContext().getFlash();
          // Put the exception in the flash scope to be displayed in the error 
          // page if necessary ...
//          flash.put("errorDetails", throwable.getMessage());
//          System.out.println("the error is put in the flash: " + throwable.getMessage());
          LOG.log(Level.SEVERE, throwable.getMessage()
                  , throwable);
//          NavigationHandler navigationHandler = fc.getApplication().getNavigationHandler();
//          navigationHandler.handleNavigation(fc, null, "error?faces-redirect=true");
//          fc.renderResponse();
      } finally {
    // Let the parent handle the rest

Extends ExceptionHandlerFactory

package com.origami.censo.jsf;

import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerFactory;

public class CustomExceptionHandlerFactory extends ExceptionHandlerFactory {
    private ExceptionHandlerFactory parent;
  public CustomExceptionHandlerFactory(ExceptionHandlerFactory parent) {
    this.parent = parent;
  public ExceptionHandler getExceptionHandler() {
    ExceptionHandler result = new CustomExceptionHandler(parent.getExceptionHandler());
    return result;

Add exception-handler-factory configuration to faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<faces-config xmlns=""


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s