Home > Archive > Java Help > December 2006 > [returning error message on abort]how to interrupt normal process in a method
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
[returning error message on abort]how to interrupt normal process in a method
|
|
| Daniel Moyne 2006-12-16, 7:11 pm |
| 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.
| |
| Andrew Thompson 2006-12-16, 7:11 pm |
| 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.
| |
| Daniel Moyne 2006-12-16, 7:11 pm |
| Andrew Thompson wrote:
> Daniel Moyne wrote:
>
> /**
> @throws ClassNameMissingException if no class names found
> */
> public String[] getAllClassName(Indi indi)
> throws ClassNameMissingException {
>
> ....
>
> System.err.println(
> "ERROR: empty _CLAS tag found for :"+indi);
> throw new ClassNameMissingException();
> ....
>
> class ClassNameMissingException extends Exception {
> ClassNameMissingException() {
> }
> }
>
> // 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();
> }
>
>
> Different to what? Another (OOP) language?
> A procedural language?
> Your understanding of Java at this moment?
>
> Andrew T.
yes Andrew I was thinking of a produral language like Perl oa a Basic ; here
it is different more abstract.
Thanks for this great idea : I have to pull my sleeves up now !
| |
| Daniel Moyne 2006-12-17, 7:08 pm |
| Andrew Thompson wrote:
> Daniel Moyne wrote:
>
> /**
> @throws ClassNameMissingException if no class names found
> */
> public String[] getAllClassName(Indi indi)
> throws ClassNameMissingException {
>
> ....
>
> System.err.println(
> "ERROR: empty _CLAS tag found for :"+indi);
> throw new ClassNameMissingException();
> ....
>
> class ClassNameMissingException extends Exception {
> ClassNameMissingException() {
> }
> }
>
> // 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();
> }
>
>
> Different to what? Another (OOP) language?
> A procedural language?
> Your understanding of Java at this moment?
Andrew T.
I tried this :
to initiate error in various methods :
throw new ClassNameMissingException(indi,1);
with the following class definition to instanciate an error :
public class ClassNameMissingException extends Exception {
public ClassNameMissingException(Indi indi,int errornumber) {
this.errornumber=errornumber;
this.indi=indi;
}
public int getErrorNumber() {
return errornumber;
}
public Indi getErrorIndi() {
return indi;
}
}
but :
(1) first of all no way to use "this" in the constructor ! ; maybe it is not
possible in this context ?
(2)then how and to get errornumber and indi known in methods
getErrorNumber() and getErrorIndi() if I have to provide respectively indi
and errornumber as arguments which will not help in catch section because I
cannot provide what I am looking for! :
catch(ClassNameMissingException cnme) {
//decide what to do here, usually a good thing is..
cnme.printStackTrace();
Indi ErrorIndi=ClassNameMissingException.getErrorIndi();
Int ErrorNumber=ClassNameMissingException.getErrorNumber();
}
Maybe I have to go though the cnme object to extract this information.
Sorry it looks like I am really stupid !
Daniel.
| |
| Daniel Moyne 2006-12-17, 7:08 pm |
| Lew wrote:
> Daniel Moyne wrote:
>
> The class as shown does not contain instance variables "errornumber"
> (should be spelled "errorNumber") or "indi". Hence you cannot in the
> constructor use, for example,
>
> this.indi=indi;
>
> You need to add the instance variable:
>
> private Indi indi;
>
> What is class Indi? If it is a business-logic component it probably should
> not be an Exception member, although there are justifications for some
> lightweight objects. If it holds any resources (file handles, database
> connections, ...) it must not be an Exception member.
Indi is a class defined for an Individual in Genealogy ; my app is a pluggin
of a existing Genealogy app where this Indi class is set.
Where do you want me to set this line :
private Indi indi;
> Sometimes Exception classes do define additional elements, such as the
> "SQLState" attribute of java.sql.SQLException. Note that "SQLState" is
> lightweight and holds no resources.
>
> You should re-define at least the default and String-argument Exception
> constructors.
>
> public ClassNameMissingException();
> public ClassNameMissingException( String message );
Here I followed Andrew'logics to avort a method process by creating an
exception which is very clever but I need to collect information related to
my error.
You mean here that I have to replace the line :
public class ClassNameMissingException extends Exception {
by : this line :
public ClassNameMissingException( String message )....
If it is not possible to use the exception trick to transfer information to
the caller for him to process the error (software error used to detect
incoherency in a database) detected I will have to use something else.
> lest inherited methods like "getMessage()" cause difficulty, and
> define "void setIndi( Indi indi )" (assuming Indi is light enough)
> and "void setErrorNumber( int en )".
>
> (Error numbers are probably not very useful to track, since they introduce
> a dependency on the external resource that maps such numbers to their
> meanings. That is the trouble with "SQLState".)
>
> You might find that you can encode everything you need in the message and
> will have no need for extra attributes. An Exception should be nearly
> atomic, almost immutable and quite independent.
>
> - Lew
| |
| Daniel Moyne 2006-12-18, 7:12 pm |
| NNTP-Posting-Host: dyn-88-121-62-66.ppp.tiscali.fr
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7Bit
X-Trace: news.tiscali.fr 1166475061 28884 88.121.62.66 (18 Dec 2006 20:51:01 GMT)
X-Complaints-To: abuse@libertysurf.fr
NNTP-Posting-Date: Mon, 18 Dec 2006 20:51:01 +0000 (UTC)
User-Agent: KNode/0.10.4
Xref: number1.nntp.dca.giganews.com comp.lang.java.help:282668
Lew wrote:
[color=darkred]
> Lew wrote:
>
> Daniel Moyne wrote:
>
> Inside class ClassNameMissingException as an instance variable.
>
> But not if it is too big. If it is too big, describe the specific problem
> in the message and do not store the Indi.
>
> Daniel Moyne wrote:
>
> No.
>
> I mean inside the class definition you need to define constructors.
>
> public class ClassNameMissingException extends Exception
> {
> private Indi indi;
>
> public ClassNameMissingException( String message )
> {
> super( message );
> }
>
> public ClassNameMissingException()
> {
> }
>
> public ClassNameMissingException( String message, Indi indi )
> {
> this( message );
> this.indi = indi;
> }
>
> public final void setIndi( Indi indi )
> {
> this.indi = indi;
> }
>
> public final Indi getIndi()
> {
> return indi;
> }
>
> }
>
> Don't forget Javadoc comments, omitted here for brevity.
>
> - Lew
>
Lew,
great it works ; I just replaced "this(message)" by "this.message=message"
to recover my string message and added "private String message" ; maybe
rather than collecting a string I will collect an int errorNumber and
decode this in the catch {} section to display the error message
accordingly but this will make my code not very clear to read ; I will see
A few questions :
- why do you have to declare indi (and message) as private,
- why do we have to set this :
public ClassNameMissingException(String message) {
super(message);
}
- I do not need here setIndi(Indi indi) as here I wait for the error to
pop-up to collect the indi same for the message string ; as a matter of
fact I am wondering where this could be used in an exception process :
(a) inside this catch section :
catch(ClassNameMissingException errorVariable) {
//decide what to do here, usually a good thing is..
errorVariable.printStackTrace();
println(errorVariable.getErrorMessage()+errorVariable.getErrorIndi());
}
no use !
(b) outside this catch section :
I have no object to apply setIndi(indi) to !!!!!
- was this exception procees the best compromise to combine abortion and
comprehensive error message related to the location where the error was
enforced (here not error system but user errors).
Thanks again.
|
|
|
|
|