S2 Tip - Use Exception handlers

Since Action classes tend to access the same business layer, most Actions often need to catch the same set of exceptions. Rather than sprinkle Actions with nearly identical try..catch blocks, configure an Exception handler to catch whatever exceptions an Action may throw.

<struts>

<global-results>

    <result name="error">/Error.jsp</result>

  </global-results>`  <global-exception-mappings>

    <exception-mapping

      result="error"

      exception="java.lang.Throwable"/>

  </global-exception-mappings>

  <!-- ... -->

&lt;/struts&gt;</pre>
Exception handlers can be either global or local to an action mapping.
<pre>`&lt;struts&gt;

  &lt;action name="Login"  class="actions.Login"&gt;

    &lt;!-- ... --&gt;

    &lt;result name="expired" type="chain"&gt;

      ChangePassword

    &lt;/result&gt;

    &lt;exception-mapping

      exception="dao.ExpiredPasswordException"

      result="expired"/&gt;

  &lt;/action&gt;

  &lt;!-- ... --&gt;

&lt;/struts&gt;

Use of exception handlers separates concerns and reduces redundant code. Each Action has fewer lines of code to maintain, and we know that exceptions are being handled consistently.