21-Jan-06 (Created: 21-Jan-06) | More in 'Howto'

Managing global exceptions in Aspire

Base servelet in aspire will catch all possible exceptions and delegate their handling to a user specified java class representing the following interface


public interface IGlobalExceptionHandler 
{

    public static String NAME="IGlobalExceptionHandler";
    /***************************************************************************
     * global exception support
     ***************************************************************************
     */
    public IOException dealWithIOException(IOException x
            ,String user
            ,HttpSession session
            ,Hashtable parameters
            ,PrintWriter out
            ,HttpServletRequest request
            ,HttpServletResponse response );
    
    public  ServletException dealWithServletException(ServletException x
            ,String user
            ,HttpSession session
            ,Hashtable parameters
            ,PrintWriter out
            ,HttpServletRequest request
            ,HttpServletResponse response );
    
    public  Throwable dealWithThrowable(Throwable t
            ,String user
            ,HttpSession session
            ,Hashtable parameters
            ,PrintWriter out
            ,HttpServletRequest request
            ,HttpServletResponse response );
}

The exceptions are caught in that order. I have preserved their signatures because a servlets run method expects io and servelt exceptions. The base servlet will throw the exception up the chain if the handlers return an exception. If they return null, the base servlet assumes the exceptions are handled and will not raise that exception.

You can specify your own handler as follows


request.IGlobalExceptionHandler.classname=yourcompany.YourGlobalExceptionHandler

Default exception handler

Here is the behavior of the default exception handler


public class DefaultGlobalExceptionHandler implements IGlobalExceptionHandler
{

    /***************************************************************************
     * global exception support
     ***************************************************************************
     */
    public IOException dealWithIOException(IOException x
            ,String user
            ,HttpSession session
            ,Hashtable parameters
            ,PrintWriter out
            ,HttpServletRequest request
            ,HttpServletResponse response )
    {
        AppObjects.log("Error: IO Exception caught in BaseServlet for URI:" 
		     + request.getRequestURI(),x);
        return x;
    }
    public ServletException dealWithServletException(ServletException x
            ,String user
            ,HttpSession session
            ,Hashtable parameters
            ,PrintWriter out
            ,HttpServletRequest request
            ,HttpServletResponse response )
    {
        AppObjects.log("Error: Servlet Exception caught in BaseServlet for URI:" 
		   + request.getRequestURI(),x);
        return x;
    }
    
    public Throwable dealWithThrowable(Throwable t
            ,String user
            ,HttpSession session
            ,Hashtable parameters
            ,PrintWriter out
            ,HttpServletRequest request
            ,HttpServletResponse response )
    {
        AppObjects.log("Error: Throwable caught in BaseServlet for URI:" 
		   + request.getRequestURI(),t);
        PrintUtils.writeException(
                ServletUtils.getSuitablePrintWriter(request,response,out)
                ,t );
        return null;
    }
}

This implementation reflects the original design of Aspire where io and servlet exception are thrown back and any other exceptions are written to the screen by aspire itself.

One alternative could be you redirect it to your own page for all exceptions.

Or throw back the throwable as well and use the web containers error page.

Build information

This is going to be available in the next build