JSF Exception Handler for exception types


Mapping different exception types to different error pages, in a JSF application


This recipe will show us how to use JBoss Solder to implement an exception handler that sends the user to a specific page, when an exception is thrown on any JSF backing bean call

How to

First, and assuming that you are working on a Maven project, you have to define the JBoss Solder dependencies, as the following:

<project xmlns="http://maven.apache.org/POM/4.0.0" 
    <!-- Solder -->

    <!-- Exception handling -->

Next, we implement our exception handler, as follows, where we specify the class as being an exception handler through the specification of the annotation HandlesExceptions from Solder and, inside of it, we add new handler methods (marked as such through the definition of the annotation @Handles associated to the method argument CaughtException parameterized with the exception type we want to deal with)) for each exception type we want to deal with (in the example, only the exception ViewExpiredException has been taken care of.

import javax.faces.application.ViewExpiredException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jboss.solder.exception.control.CaughtException;
import org.jboss.solder.exception.control.Handles;
import org.jboss.solder.exception.control.HandlesExceptions;
import org.jboss.solder.servlet.WebRequest;

public class ExceptionHandlers {

  public void handleViewExpiredException(
    @Handles(precedence = org.jboss.solder.exception.control.Precedence.HIGH) 
    @WebRequest CaughtException<ViewExpiredException> caught,
    final HttpServletRequest request, final HttpServletResponse response) {
    try {
        request.getContextPath() + "/errors/timeout.xhtml"));
    } catch (Exception e) {

Notice additionally, in the previous method implementation, the call to caught.handled() which tells Solder that the exception type has already been taken care of and thus no other exception handler methods should treat this exception again.

Finally, and although not necessary, we may add the definition for the handling of the generic Java exception Exception in our web.xml (or corresponding web-fragment.xml) file and where we can also add a note for other developers to be aware of that the class ExceptionHandlers exist and what it does:

<!-- See class ExceptionHandlers for more exception handler cases -->

To test this implementation, we can now create a backing bean like the following:

public class CatastrophyBean {

  public void generateViewExpiredException() {
    throw new ViewExpiredException();

and the corresponding page to interact with the bean:

    value="Generate View Expired Exception" ajax="false" />


In JSF, we can map error pages to HTTP error codes with no problems. However, when we do that to exceptions, those are wrapped around other JSF exceptions that force us to implement some sort of exception handler (see, for instance, a sample implementation in coderanch). With JBoss Solder at use, we get a simple solution to map any type of exception to specific pages, as wanted


Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s