posted 18 years ago
The advantage to using the ExceptionHandler over simply catching the exception in the Action class is this:
One of the main reasons for using the Struts Global Exception feature is to avoid having to put try/catch blocks in every Action class. If we use an ExceptionHandler, we can still avoid try/catch blocks because the ExceptionHandler gets called by Struts when it catches an exception. If we catch the exception in every action class, we really don't need to use the global exception feature, because we've already caught the exception ourselves and can handle it. We don't need Struts to catch it for us.
Regarding your question about whether it is correct to put an Exception in the request: As far as I'm concerned, it's correct to put anything in the request that you want to put there. Generally, you want to put JavaBeans in the request, since most of the custom tags only work with JavaBeans. Even though the Exception class is not a JavaBean, the getMessage method follows the JavaBean convention, making it usable by a tag.
For example, if you implemented the code I showed above for the ExceptionHandler, there would be an Exception object in the request with the name "theException". In your JSP, you could display the exception's message with the code:
<bean:write name="theException" property="message" />
If you'd rather put an ActionMessages object into the session rather than the raw Exception class, you can still do that in the ExceptionHandler class if you want.
[ July 25, 2006: Message edited by: Merrill Higginson ]