Results 1 to 1 of 1
  1. #1
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Rep Power

    Default Adding global exception handling using JSF 2.x ExceptionHandler


    i have the following classes to handle any error could happen in JSF pages.

    Java Code:
    import java.util.Iterator;
    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.event.ExceptionQueuedEvent;
    import javax.faces.event.ExceptionQueuedEventContext;
    public class CustomExceptionHandler extends ExceptionHandlerWrapper {
        private ExceptionHandler wrapped;
        public CustomExceptionHandler(ExceptionHandler wrapped) {
            this.wrapped = wrapped;
        public ExceptionHandler getWrapped() {
            return wrapped;
        public void handle() throws FacesException {
            Iterator i = getUnhandledExceptionQueuedEvents().iterator();
            while (i.hasNext()) {
                ExceptionQueuedEvent event = (ExceptionQueuedEvent);
                ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
                // get the exception from context
                Throwable t = context.getException();
                FacesContext fc = FacesContext.getCurrentInstance();
                try {
                    /* Here you can use the Throwable object in order to verify the exceptions you want to handle
                     in the application */
                    NavigationHandler navigationHandler = fc.getApplication().getNavigationHandler();
                    navigationHandler.handleNavigation(fc, null, "error?faces-redirect=true");
                } finally {
    // Call the parent exception handlerís handle() method
    CustomExceptionHandler Class, which handels with errors.
    If any exception is thrown, it will redirect to error.xhtml
    Java Code:
    import javax.faces.context.ExceptionHandler;
    import javax.faces.context.ExceptionHandlerFactory;
    public class CustomExceptionHandlerFactory extends ExceptionHandlerFactory {
        private ExceptionHandlerFactory parent;
    // this injection handles jsf
        public CustomExceptionHandlerFactory(ExceptionHandlerFactory parent) {
            this.parent = parent;
        public ExceptionHandler getExceptionHandler() {
            ExceptionHandler result = new CustomExceptionHandler(parent.getExceptionHandler());
            return result;
    And add the following lines to your faces-config.xml:
    XML Code:
    the error.xhtml under web pages
    XML Code:
    <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE html>
    <html xmlns=""
            <h:outputStylesheet library="css" name="JSF-Styles.css"  />
            <h2 class="errorMessage">
                An error occurs. return to <a href="index.xhtml">login</a> page.
    Any way when i make (on purpose) any mistake in any xhtml page (like deleting close tag ">"), but no thing happens
    just page with HTTP Status 500 - but my error.xhtml isn't displayed.

    Reference: Adding global exception handling using JSF 2.x ExceptionHandler
    Any suggestions ??
    Last edited by vector_ever; 07-31-2015 at 10:14 PM.

Similar Threads

  1. Exception Handling
    By teja in forum New To Java
    Replies: 6
    Last Post: 05-15-2013, 07:41 PM
  2. exception handling
    By Elaforestis in forum New To Java
    Replies: 1
    Last Post: 01-29-2013, 05:37 AM
  3. Exception handling
    By Kartiky14 in forum New To Java
    Replies: 3
    Last Post: 03-25-2012, 09:07 AM
  4. Exception handling
    By ExX in forum New To Java
    Replies: 6
    Last Post: 03-12-2012, 12:48 PM
  5. Exception handling
    By herat in forum New To Java
    Replies: 1
    Last Post: 06-21-2011, 11:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts