How to return error message and handle it decently

In a java web application environment, when I want to do a job such as register a user, assume that I can do it manually, that is in a set of sequent jsp pages, I fill some infomations in every page and click "next" button to next page, or I still must make it a single step - just import a setting file which contains all the infomations that I must fill when registering a new user.

assume the register page is named : reg.jsp

when I choose register from importing a setting file and click submit, the RegFromFileAction servlet will be called to check whether the file is valid and then parse it.

When checking this setting file, many errors may occurs, so I want handle all error message in the same way, because there will be very kind of error messages here, such as : "Error: invalid file type", "Error: invalid user name in line 3".

my code is as followig:

First I have made a exception call RegException

public class RegException extends Exception {

private String errmsg = null;

/**

* constructor

*/

public CreatePrjException() {

super();

}

/**

* constructor

* @param s

*/

public CreatePrjException(String s) {

super(s);

errmsg = s;

}

/**

* @return Returns the errmsg.

*/

public String getErrmsg() {

return errmsg;

}

}

in RegFormFileAction servlet:

the main skeleton is:

try {

if (file.isValid()) {

UserInfoBean uib = file.getInfoAsBean();

}

} catch (RegException e) {

request.setAttribute("ERR_MSG", e.getMeesage());

//go to error.jsp and display the error message

}

when I find some error in the file, I throw an exception in file.isValid() like this:

throw new Exception("The file is not valid");

and I can also throw some exception in file.getInfoAsBean();

So I think with exception, I can handle all error message in the same way.

but using exception will be not efficient, and all the error message can't be managed in a same way, because they are existing in every method.

Is there any better solutions?

Thank you for your help!

[2227 byte] By [flypig] at [2007-9-30 20:40:46]
# 1

Arrrggh. Efficiency!

Look, how many exceptions are likely to be thrown on a particular page? A maximum of one, typically, because the page processing will stop. And in the vast majority of cases, no exceptions at all will be thrown. The expense of exception handling (which is exaggerated to begin with) really doesn't matter.

Exceptions are in the language to handle exactly the case you are talking about. Use the tool in your hand.

Oh, and Exception already has an error message.

Michael_Lorton at 2007-7-7 1:29:51 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

So, can you give some good advice?

Because I just want to handle error message in the same way.

I can use exception do that because there will be many error message caused in many functions, when I call these functions, I want to handle all error messge in the same way.

It is not reasonable to use return value, because in some function I only want to know the result is true of false.

flypig at 2007-7-7 1:29:51 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

Hi,

Not sure I totally understand the question but could you not just create a static utility class for handling RegExceptions, so your catch code would look something like:

} catch (RegException e) {

// ...

RegExceptionHandler.handleException( e )

// ...

}

Ol.

0s at 2007-7-7 1:29:51 > top of Java-index,Other Topics,Patterns & OO Design...
# 4
I agree, just be careful not to be tempted into catching Throwable (or Error). You can do it in a standalone application, but it is inadvisable to catch Throwable in a J2EE container. Exception is as 'low' as you should go.- Saish"My karma ran over your dogma." - Anon
Saish at 2007-7-7 1:29:51 > top of Java-index,Other Topics,Patterns & OO Design...