Re: [returning error message on abort]how to interrupt normal process in a method
Lew wrote:
Lew 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;
Daniel Moyne wrote:
Where do you want me to set this line :
private Indi indi;
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:
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 )....
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
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,
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.