spring exception handler to not handle certain types of exception

One way to handle this is to create another handler implementing the org.springframework.web.servlet.HandlerExceptionResolver and org.springframework.core.Ordered -interfaces. In your own implementation, you do something like the following:

public class AccessDeniedExceptionResolver implements HandlerExceptionResolver, Ordered
{
    private int order;

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception)
    {
        if(exception instanceof AccessDeniedException)
        {
            return new ModelAndView("redirect:/login"); //Replace with redirect to your login-page
        }

        return null; //Null-return = pass the exception to next handler in order
    }

    public void setOrder(int order)
    {
        this.order = order;
    }

    @Override
    public int getOrder()
    {
        return order;
    }   
}

Now, the Ordered-interface implementation allows you to tell the order the exception handlers are invoked in. The SimpleMappingExceptionResolver implements the Ordered interface also, so you could do something like following in your bean-definitions:

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
        <props>
            <prop key="java.lang.RuntimeException">common/error</prop>
        </props>
    </property>
    <property name="order" value="1"/>
</bean>

<bean class="package.to.your.handler.AccessDeniedExceptionResolver">
    <property name="order" value="0"/>
</bean>

The bean with the LOWER order-value has HIGHER priority (meaning it will be invoked BEFORE the ones with larger values, in this case AccessDeniedExceptionResolver is invoked before SimpleMappingExceptionResolver.

Hope this helped.


The way we handle this is to have a custom exception resolver class that handles any exceptions that are not caught by other handlers - it implements HandlerExceptionResolver, Ordered.

We declare a separate SimpleMappingExceptionResolver bean that catches specific exceptions.

The ordering is such that our custom resolver runs after the SimpleMappingExceptionResolver.

The effect is that specified exceptions (e.g. AccessDeniedException) are handled by SimpleMappingExceptionResolver and directed to the appropriate pages.

Any other run time exceptions are handled by the custom resolver, which forwards to a generic error page.

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
        <props>
            <prop key="org.springframework.security.AccessDeniedException">accessDenied</prop>
            <prop key="org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException">accessDenied</prop>
        </props>
    </property>
    <property name="order" value="0"/>
</bean>

<bean class="package.to.your.handler.DefaultExceptionResolver">
    <property name="order" value="1"/>
</bean>

This arrangement allows you to catch as many exceptions as you like (I catch 2 here, AccessDenied and HibernateOptimisticLockingFailureException) using the Spring resolver and everything else is caught by the custom resolver. In the accepted solution above, you would have to write more Java code to catch exceptions other than AccessDenied.