[returning error message on abort]how to interrupt normal process in a method
I have a method that return an array, something like this :
public String[] getAllClassName(Indi indi) {
/* check for existing classes */
/* not very efficient as we do not have a class names list */
ArrayList<String>classnamelist=new ArrayList<String>();
String classname;
Boolean errorflag=false;
Property classnameproperties[];
Indi indi_;
Gedcom gedcom=indi.getGedcom();
Collator myCollator =gedcom.getCollator();
Collection indis=gedcom.getEntities(Gedcom.INDI);
for (Iterator it=indis.iterator(); it.hasNext();) {
/* we get all _CLAS properties from all INDI's */
indi_=(Indi)it.next();
/* we get all _CLAS property values from all INDI's */
classnameproperties=indi_.getProperties(ClassNameTag);
/* if no _CLAS tags we do nothing */
if (classnameproperties.length != 0) {
/* there are _CLAS tags */
/* we want to collect the _CLAS tag value */
for (int i=0; i<classnameproperties.length; i++) {
classname=classnameproperties[i].getValue();
if (classname.equals("")) {
/* here we want to abort */
println("ERROR: empty _CLAS tag found for :"+indi);
break; /* is this good */
}
else {
if (!classnamelist.contains(classname)) {
/* classname not included yet in classnamelist */
/* we add it */
classnamelist.add(classname);
}
}
}
if (classnameproperties.length == 2) {
/* here we want to abort */
println("ERROR: more than one _CLAS tag found for :"+indi);
break;
}
}
}
/* we sort classnamenamelist alphabetically */
Collections.sort(classnamelist,myCollator);
return classnamelist.toArray(new String[classnamelist.size()]);
}
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 error message can either :
- (1) be processed inside the method then in this case we just need to
terminate properly the called method but returning what ? : a dummy array
(null ?) for notification,
- -(2) or outside of the method(s) by getting an error number generated by
the method that avorts ; in this case the main advantage is that you have a
general method that can process error messages but we still have to collect
this error number from the method.
Of course I can have a static "int ErrorNumber =0" set up front and each
method can set it with its own error number but is this the best way to
proceed ?
With Java I am a little puzzled as the way to think is different.
Thanks.