7 Replies Latest reply on Apr 22, 2011 6:47 AM by Ilya Shaikovsky

    When use a4j.ajax,How to catch Exception and show it on pages?

    maxim rong Newbie

      HI, I use richfaces4 and jsf2.

      when use jsFunction or jsCommandButton, how to catch the exception from serivce, and show it on page?

      the page like follow:



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

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

      <html xmlns="http://www.w3.org/1999/xhtml"





            xmlns:f="http://java.sun.com/jsf/core" >




              <meta http-equiv="content-type" content="text/html; charset=UTF-8" />

                <script type="text/javascript">


                window.onload = function() {

                             // jsFunction



                 // -->





            <h:form prependId="false">

              <a4j:jsFunction name="login" action="#{userInfo.login}">







      and the UserInfo like this:



      public class UserInfo {


      public void login() throws Exception {

              throw new Exception("test!!");





      i want to catch the exception and show it on page,

      i try to use

      A4J.AJAX.onError = function(req, status, message)  {




      but there is a js error:

      A4J is not defined!

        • 1. Re: When use a4j.ajax,How to catch Exception and show it on pages?
          Rocky S Master

          refer to, how to use javascript to handle exceptions. Use try block outside login and catch the exception. here you go. Since you know your exception very well, handle at your convenience and take appropriate action.


          A4J.AJAX.onError is generally used when there is an error in ajax response...example to handle view expiration or no response from server.

          • 2. Re: When use a4j.ajax,How to catch Exception and show it on pages?
            maxim rong Newbie

            hi, Aman!

            i just tried use try catch outside login (js), but i can't catch the exception from action.

            and i just want to know how to catch the exception in ajax response.

            so i try to user A4J.AJAX.onError.

            but it said A4J is not defined,

            Whether i need to include some js file?


            or there is some other way to catch exception from action.(via jsf2 or richfaces)

            • 4. When use a4j.ajax,How to catch Exception and show it on pages?
              maxim rong Newbie


              i want to catch the exception msg from action, and alert is in js.like "throw new Exception("test!!")";

              could you gave me an example??

              • 5. When use a4j.ajax,How to catch Exception and show it on pages?
                Ilya Shaikovsky Master

                Client side

                1) http://javaserverfaces.java.net/nonav/docs/2.0/jsdocs/symbols/jsf.ajax.html#constructor

                2) RichFaces components provides onerror handler at component level additionally.


                Server Side

                Read about JSF 2 exception handling there http://andyschwartz.wordpress.com/2009/07/31/whats-new-in-jsf-2/ and there http://javaserverfaces.java.net/nonav/docs/2.0/javadocs/javax/faces/context/ExceptionHandler.html and also could check some JSF 2 reference for real samples.

                • 6. Re: When use a4j.ajax,How to catch Exception and show it on pages?
                  maxim rong Newbie

                  thx a lot,

                  but i'm sorry that i can't access page which your provided.because my country's net is limit.

                  this problem i have done by myself, and i show my solutions.


                  first, custom the jsf's exception handler and config your exception handler in faces-config.xml.

                  and you need write two class,

                  one is ExceptionHandldFactory and it must extends ExceptionHandlerFactoryImpl.

                  follow is my Class:



                  package com.agile.sys.exception;


                  import javax.faces.context.ExceptionHandler;


                  import com.sun.faces.context.ExceptionHandlerFactoryImpl;


                  public class ExceptionHandlerFactory extends ExceptionHandlerFactoryImpl {


                      private final javax.faces.context.ExceptionHandlerFactory parent;


                      public ExceptionHandlerFactory(javax.faces.context.ExceptionHandlerFactory parent) {

                          this.parent = parent;




                      public ExceptionHandler getExceptionHandler() {

                          ExceptionHandler result = parent.getExceptionHandler();

                          result = new CustomExceptionHandler(result);

                          return result;






                  then you need config this class in your faces-config.xml.(even in jsf2, you also need to do this):








                  second, you need to write another Class:



                  package com.agile.sys.exception;


                  import java.util.Iterator;


                  import javax.faces.FacesException;

                  import javax.faces.application.FacesMessage;

                  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 final ExceptionHandler parent;


                      public CustomExceptionHandler(ExceptionHandler parent) {

                          this.parent = parent;




                      public ExceptionHandler getWrapped() {

                          return this.parent;




                      public void handle() throws FacesException {

                          // todo you exception handler, follow is mine, i add the exception to Message.

                          for (Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) {

                              ExceptionQueuedEvent event = i.next();

                              ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();

                              Throwable t = context.getException();


                                                  new FacesMessage(FacesMessage.SEVERITY_ERROR, t.getMessage(), t.getMessage()));



                          //        getWrapped().handle();






                  the last, in your pages, you can use the <h:messages>, it can show your exception.


                  my English is not good, i hope i expressed clearly,