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!

