PrimeFaces provides a powerful ExceptionHandler out of the box featuring support for ajax and non-ajax requests, ability to use error-page configuration in web.xml, an EL extension called to provide information about the exception and the p:ajaxExceptionHandler component to handle ajax exceptions at the same page.
Documentation<h:form> <h3 style="margin-top:0">AJAX</h3> <p:commandButton action="#{exceptionHandlerView.throwViewExpiredException}" ajax="true" value="Throw ViewExpiredException!" /> <p:commandButton action="#{exceptionHandlerView.throwNullPointerException}" ajax="true" value="Throw NullPointerException!" /> <!-- IllegalStateException is not handled using ajaxExceptionHandlers below, so the error page is shown--> <p:commandButton action="#{exceptionHandlerView.throwWrappedIllegalStateException}" ajax="true" value="Throw IllegalStateException!" /> <h3>Non-AJAX</h3> <p:commandButton action="#{exceptionHandlerView.throwViewExpiredException}" ajax="false" value="Throw ViewExpiredException!" /> <!-- NullPointerException has no specific error-page defined in web.xml compared to ViewExpiredException --> <!-- https://github.com/primefaces/showcase-facelift/blob/master/src/main/webapp/WEB-INF/web.xml --> <p:commandButton action="#{exceptionHandlerView.throwNullPointerException}" ajax="false" value="Throw NullPointerException!" /> <p:ajaxExceptionHandler type="javax.faces.application.ViewExpiredException" update="exceptionDialog" onexception="PF('exceptionDialog').show();" /> <p:ajaxExceptionHandler type="java.lang.NullPointerException" update="exceptionDialog" onexception="PF('exceptionDialog').show();" /> <p:dialog id="exceptionDialog" header="Exception '#{pfExceptionHandler.type}' occured!" widgetVar="exceptionDialog" height="500px"> Message: #{pfExceptionHandler.message} <br/> StackTrace: <h:outputText value="#{pfExceptionHandler.formattedStackTrace}" escape="false" /> <br /> <p:button onclick="document.location.href = document.location.href;" value="Reload!" rendered="#{pfExceptionHandler.type == 'javax.faces.application.ViewExpiredException'}" /> </p:dialog> </h:form>
@Named @RequestScoped public class ExceptionHandlerView { public void throwNullPointerException() { throw new NullPointerException("A NullPointerException!"); } public void throwWrappedIllegalStateException() { Throwable t = new IllegalStateException("A wrapped IllegalStateException!"); throw new FacesException(t); } public void throwViewExpiredException() { throw new ViewExpiredException("A ViewExpiredException!", FacesContext.getCurrentInstance().getViewRoot().getViewId()); } }