aspose file tools*
The moose likes Struts and the fly likes Questions about Exception handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Questions about Exception handling" Watch "Questions about Exception handling" New topic
Author

Questions about Exception handling

Artemesia Lakener
Ranch Hand

Joined: Jun 21, 2005
Posts: 162
I just have normal Exceptions(like ServletException, IOException, SQLException) to handle and I don't need to do too fancy things. Here are my questions ---

1. if I ONLY type <html:error/> in each of my JSP pages without defining <global-exception> in the xml file, will I still be able to see all the error messages ?

2. Is it true that the only thing <global-exception> setting can buy me is to allow me to dispatch to customized different error pages for different exceptions (the exception can be simple ServletException) ? Then in this case, what's usage of <html:errors/> since the error page will be used anyway ?

3. In javax servlet API we have a servlet.log() method, since we use struts, can we still use this "servlet.log()" method ? and how ? I thought we can't because we usually don't touch servlet by ourselves any more..


thanks
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
1. If you only put <html:errors /> in your page without defining global exceptions, the only mesages that will appear are ones that you add using the ActionMessages add() method for that specific acion. Struts will not put any exceptions there, nor forward back to your page if an exception occurs. Any unhandled exceptions will result in Struts using sending a generic error page to the browser.

2. If you want the error page to show a generic message such as "something went wrong", then there's no need to use <html:errors> in your error page. If you want to inform the user of the specific exception that was thrown, then use <html:errors /> to show the specific exception that ocurred. Most web developers will agree, however, that it's not generally a good idea to send java exception messages to users of a website. It give the impression of an amateurish site, and often makes the user worry that there's more going wrong than there actually is.

3. The struts Action class defines a getServlet() method that will return the servlet that called the action. You can then call the log() method on the servlet. There are more sophisticated logging mechanisms available through the apache.commons.logging package that comes with Struts.


Merrill
Consultant, Sima Solutions
Artemesia Lakener
Ranch Hand

Joined: Jun 21, 2005
Posts: 162
So, if I want to direct to a genaral error page showing what type of error it is, do you think the best way is to 1) use <html:errors/> and 2) in struts-config.xml do:

<global-exceptions>
<exception key="database.key" path="/generalerror.jsp"
scope="request" type="SQLException"/>

<exception key="servlet.error" path="/generalerror.jsp"
scope="request" type="ServletException"/>

And in generalerror.jsp I do not need to retrieve any specific error type, etc as the "key" will output corresponding error msg for me. Is that right ?

One more question --- Since I use both "Global exception" and <html:errors/>, when SQLException rises, will it follow the global exception's generalerror.jsp or follow <html:errors/> to a white page ? any conflict here ?

Plus, no matter what I define, for a specifc Action, if I just want to treat SQLException in "that" Action class differently, I can still choose to catch it in the class and handle it differently, right ? In this case, the global-exception defined for SQLException will be used for all the SQLExceptions occured in other Action classes, right ?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The way it works is that you code a <html:errors/> tag inside the generalerror.jsp. When the Struts ActionServlet catches an exception that you haven't caught in your Action's execute() method, it will forward to the generalerror.jsp page, and the error message you define by the key attribute will be rendered by the <html:errors/> tag.

Struts Exception handling only applies to un-caught exceptions, so naturally if you want to handle an exception before Struts does, just include it in a catch block in your execute() method. That way one action can handle an SQLException while another doesn't handle it and passes it on to Struts to handle.
[ July 01, 2005: Message edited by: Merrill Higginson ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Questions about Exception handling