Apache Struts 2 Documentation > Home > Guides > Core Developers Guide > Struts Configuration Elements > Exception Configuration
Added by matthew, last edited by Ted Husted on Sep 03, 2006  (view change)

Exception mappings is a powerful feature for dealing with an Action that throws an Exception. The core idea is that an Exception thrown during the Action method can be caught and mapped to a Result. This strategy is especially useful for frameworks, like Hibernate and Acegi, that throw RuntimeExceptions.

As with many other parts of the framework, an Interceptor is needed to activate the exception mapping functionality. Below is a snippet from struts-default.xml which has the exception mapping already activated.

snippet of struts-default.xml
...
<interceptors>
    ...
    <interceptor name="exception" class="com.opensymphony.xwork.interceptor.ExceptionMappingInterceptor"/>
    ...
</interceptors>

<!-- Basic stack -->
<interceptor-stack name="basicStack">
    <interceptor-ref name="exception"/>
    <interceptor-ref name="servlet-config"/>
    <interceptor-ref name="prepare"/>
    <interceptor-ref name="static-params"/>
    <interceptor-ref name="params"/>
    <interceptor-ref name="conversionError"/>
</interceptor-stack>
...

The next step in exception mapping is to actually map Exceptions to specific Results. The framework provides two ways to declare an exception mapping <exception-mapping/> - globally or for a specific action mapping. The exception mapping element takes two attributes, exception and result.

When declaring an exception mapping, the Interceptor will find the closest class inheritance match between the Exception thrown and the Exception declared. The Interceptor will examine all declared mappings applicable to the action mapping, first local and then global mappings. If a match is found, the Result is processed, just as if it had been returned by the Action.

This process follows the same rules as a Result returned from an Action. It first looks for the Result in the local action mapping, and if not found, it looks for a global Result.

Below is an example of global and local exception mappings.

snippet from struts.xml
<xwork>
    <package name="default">
        ...
        <global-results>
            <result name="login" type="redirect">/login.action</result>
            <result name="rootException" type="freemarker">/WEB-INF/views/exception.ftl</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.sql.SQLException" result="sqlException"/>
            <exception-mapping exception="java.lang.Exception" result="rootException"/>
        </global-exception-mappings>
        ...
        <action name="myAction" class="...">
            <interceptor-ref name="exception" />
            <exception-mapping exception="com.acme.foo.SecurityException" result="login"/>
            <result name="sqlException" type="chain">sqlExceptionAction</result>
            <result name="success" type="freemarker">/WEB-INF/views/acme/success.ftl</result>
        </action>
        ...
    </package>
</xwork>

In the example above, here is what happens based upon each Exception:

  • A java.sql.SQLException will chain to the sqlExceptionAction
  • A com.acme.foo.SecurityException will redirect to login.action
  • Any other exception that extends java.lang.Exception will execute the FreeMarker result rootException for the page /WEB-INF/views/exception.ftl

Exception Values on the ValueStack

By default, the ExceptionMappingInterceptor adds the following values to the Value Stack:

exception The exception object itself
exceptionStack The value from the stack trace