Re: [returning error message on abort]how to interrupt normal process in a method
Daniel Moyne wrote:
I have a method that return an array, something like this :
public String[] getAllClassName(Indi indi) {
/**
@throws ClassNameMissingException if no class names found
*/
public String[] getAllClassName(Indi indi)
throws ClassNameMissingException {
/* check for existing classes */
.....
if (classname.equals("")) {
/* here we want to abort */
println("ERROR: empty _CLAS tag found for :"+indi);
break; /* is this good */
System.err.println(
"ERROR: empty _CLAS tag found for :"+indi);
throw new ClassNameMissingException();
}
else {
.....
}
class ClassNameMissingException extends Exception {
ClassNameMissingException() {
}
}
So when everything goes well I normally get an array of string values but in
some cases I want to abort the method and return an error message ; as a
method can just return an object (?) how to proceed neatly for the caller
of the method to get either :
- the array he wants,
- or possibly a notification of an error when aborting.
// the caller 'try's something that might fail..
try {
String[] names = getAllClassName(Indi indi);
// proceed with processing the class names
// ....
} catch(ClassNameMissingException cnme) {
//decide what to do here, usually a good thing is..
cnme.printStackTrace();
}
With Java I am a little puzzled as the way to think is different.
Different to what? Another (OOP) language?
A procedural language?
Your understanding of Java at this moment?
Andrew T.