File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes exception handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "exception handling" Watch "exception handling" New topic

exception handling

Artemesia Lakener
Ranch Hand

Joined: Jun 21, 2005
Posts: 162
I wrote a web app using struts, I have JSP that calls some Action classes and eventually forward to other JSPs. Assuming I don't want to catch any of the exceptions in the Action classes, how should "implement" the error handling ? A fellow suggest I just type the dummy </html:erros> in every JSP pages, does that make sense ? should I define some <global exception> in the struts-config.xml ?
martin jose

Joined: Aug 10, 2005
Posts: 9

What type of exceptions you want to handle?
<html:errors> is normally used for form validation.

<html:text property="name"/><html:errors property="name"/>

you can put code like this in the JSP, so that if there is any error with the data you entered in the name field can be notified to the user.

if you simply put <html:errors> , it will display all the errors.

You must create this error entries either in your Form or Action.

ActionErrors errors = new ActionErrors();
if (name.length()==0 || name == null)
errors.add("name",new ActionError("error.emptychk"));
return errors;

if you want handle other error cases associated with the action, you can put the code in try-catch blocks and inside the block you can write the code for forwarding to a corresponding error pages


catch(Exception e)

Also you can associate some Exceptions with an action. You can define each oof these exceptions with the help of a Custom exception class or an Existing Exception class. You can also specify where you forward on these exceptions.

Hope this will help you.

Artemesia Lakener
Ranch Hand

Joined: Jun 21, 2005
Posts: 162
I am more interested in other types of exceptions like SQLException, classnotfound exception, IO exception, etc. Since the web application includes a series of JSP and Actions (JSP1-->Action 1-->JSP 2 -->Action 2-->JSP 3 -->Action 3-->...), where should I catch the exceptions ? should I catch them in the last Action only ?
I agree. Here's the link:
subject: exception handling
It's not a secret anymore!