title: How to handle exceptions thrown in a Struts 2 user-defined AJAX result?
body: <p>I am using Struts 2 user-defined result that is used with an AJAX request.</p>
  
  <p>When it throws an exception, I get a "Struts Problem Report" in html format as a response. This is not very useful. How can I intelligently handle such an exception -- either call the appropriate javascript errorResponse function or take the user to another page?</p>
  
  <pre><code>public class MyResult implements Result {
  
      @Override
      public void execute(ActionInvocation invocation) {
  
          if (invocation.getStack().findValue("data") == null) {
              throw MyException("Data is bad.");
          }
  
          PrintWriter responseStream =
              ServletActionContext.getResponse().getWriter();
          responseStream.println("Data is good.");
          responseStream.close();
      }
  }
  </code></pre>