Handling an exception using Struts 2
Struts 2 has a neat feature where by it can automatically trap exceptions that you did not handle in your code. You can then decide what to do with this exception such as pass it off to your own custom action which can then write the exception to a database or send an email to someone. This is how it is done, in your struts.xml file you need to add:
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="unhandledExceptionHandler"/>
</global-exception-mappings>
<global-results>
<result name="unhandledExceptionHandler" type="chain">
<param name="actionName">actionUnhandledExceptionHandler</param>
<param name="namespace">/namespaceWhereActionIsDefined</param>
</result>
</global-results>
<action name="actionUnhandledExceptionHandler" class="my.package.struts2.action.UnhandledExceptionHandler">
<result name="canNotLogDatabaseError">/WEB-INF/page/error.jsp</result><!-- Used only if there is an error trying to log the exception -->
</action>
The above is pretty easy to follow once you know how it is done:
- The <global-exception-mappings> node has a child <exception-mapping .../>. The child will trap any type of exception (or you could be more specific and specify my.package.MyException) and then pass the result off to the action called unhandledExceptionHandler.
- The unhandledExceptionHandler is defined in the <global-results> section. This simply chains the action to another action, i.e. our custom action that will write the exception to the database, send an email or whatever else we tell it to do. In our case this will chain to /namespaceWhereActionIsDefined/actionUnhandledExceptionHandler
- In our package /namespaceWhereActionIsDefined we have our action definition which is just a normal Struts 2 action. As an example of such an action we may have the following (UnhandledExceptionHandler.java):
package my.package.struts2.action;
public class ActionUnhandledExceptionHandler extends BaseActionSupport {
private Exception exception;
@Override()
public String execute() {
if(exception != null){
// Write exception to database, send an email or whatever you want to do here
// ...
// ...
// Send the user to the error page with a message
addActionError(getText("common.unknown.exception") + "<div class=\"errorMessageExceptionText\">" + exception.toString() + "</div>");
return ERROR; // Everything went OK, we just want to redirect to the error page"
} else {
logger.error("***** TRIED TO LOG EXCEPTION BUT EXCEPTION WAS NULL! I HAVE NOT LOGGED THIS EXCEPTION! *****");
}
addActionError(getText("actionUnhandledExceptionHandler.could.not.log.exception"));
return "canNotLogDatabaseError";
}
/**
* @param exception the exception to set
*/
public void setException(Exception exception) {
this.exception = exception;
}
}
Struts will automatically inject the java.langException into the setter UnhandledExceptionHandler.setException() making it immediately available to your action. Finally to show the error message on your error page (/WEB-INF/page/error.jsp) just use:
<s:if test="actionErrors != null && actionErrors.size > 0">
<s:actionerror />
</s:if>
11.02.2009 07:48 - Posted by doahh - Comments: 4 - Java

Comments:
Above examples is good and useful, thanks Muthu
29.04.2009 06:42 - Posted by Ponmarimuthu - Permalink
Couldn't you access the ExceptionHolder properties in the error.jsp?
i.e.
<s:property value="exception"/>
<s:property va;ue="exceptionStack"/>
19.10.2009 12:05 - Posted by Andrew Birchall - Permalink
I think the example you gave is useful if you catch an exception and then handle it by redirecting to a JSP but what do you do if your code does not catch the exception? In that case you get an unexpected error.
I think it is generally easier and cleaner to write an error to the DB or send an email from within the action code than from the JSP. Doing it this way can notify an admin that the site has a bug in it automatically and it needs to be fixed. They could always parse their own logs of course and handle it that way but this saves them the effort.
19.10.2009 12:18 - Posted by doahh - Permalink
05.11.2009 07:22 - Posted by Unknown - Permalink