values for fields in failure/exception

M

mark jason

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;
}


regards,
mark
 
L

Lew

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.
 
A

Arne Vajhøj

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;
}

I think the whole concept is wrong.

You should not catch an exception and return an object
that by some magic values indicates and exception happened.

Let the exception propagate up to where the exception can be
really handled and avoid the magic values.

Arne
 
E

Eric Sosman

I think the whole concept is wrong.

You should not catch an exception and return an object
that by some magic values indicates and exception happened.

Let the exception propagate up to where the exception can be
really handled and avoid the magic values.

Sometimes you can do that, sometimes you can't, and sometimes
you don't want to.

The classic example of "can't" is when you're implementing an
interface and the interface's method lacks "throws SomeException".
If that's the case you cannot just let the exception propagate. Of
the many possible alternatives, three are common:

- Catch SomeException, wrap it in another exception (often an
unchecked one), and throw that instead

- Catch SomeException and "recover," perhaps by returning a
special value

- Catch SomeException and abort the program.

The third alternative is too draconian for most situations; the first
two are (IMHO) both perfectly viable, depending on the context.
 
S

Stefan Ram

mark jason said:
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.

The purely object-oriented solution is as follows:

calculate( expression, success, failure );

. »calculate« does the calculation based on the object
»expression« and then calls the »acceptResult« method of the
object »success« if the calculation was a success or the
»acceptFailure« method of the object »failure« if the
calculation failed.

A partially object-oriented solution is as follows:

interface Result {};
class SuccessResult implements Result { /* result data here */ };
....
final Result result = calculate();
if( result instanceof SuccessResult )
{ final SuccessResult successResult =( SuccessResult)result;
/* use data here */ }
else
{ /* error handling */ }

Another solution uses exceptions:

try
{ final Result result = calculate();
/* use data here */ }
catch( final CouldNotCalculateException couldNotCalculateException )
{ /* error handling */ }
 
J

Jean-Baptiste Nizet

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?

Every public class and public method defines a contract, that clients
must respect. If you define the contract clearly, by documenting it,
clients just have to obey the contract and everything will be fine.
Here's an exemple of such a contract.

/**
* The result of the computation, which can be successful or not. The
result
* contains a distance, a message and the file name of the match. The
distance
* only makes sense in the case of a successful result.
*/
public 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;
}

/**
* Gets the distance from the result. Note that the returned
distance is
* undetermined if the result is not successful.
* @see #isSuccess() to know if the result is successful and if
calling this method
* makes sense
*/
public double getDistance() {
return this.distance;
}

/**
* Determines is the result is successful or not. Clients should
call this method
* and ensure the result is successful before getting the distance
from the result.
* @return <code>true</code> if the result is successful,
<code>false</code>otherwise.
*/
public boolean isSuccess() {
return this.success;
}

// ...
}

JB.
 
A

Arne Vajhøj

Sometimes you can do that, sometimes you can't, and sometimes
you don't want to.

The classic example of "can't" is when you're implementing an
interface and the interface's method lacks "throws SomeException".
If that's the case you cannot just let the exception propagate. Of
the many possible alternatives, three are common:

- Catch SomeException, wrap it in another exception (often an
unchecked one), and throw that instead

- Catch SomeException and "recover," perhaps by returning a
special value

- Catch SomeException and abort the program.

The third alternative is too draconian for most situations; the first
two are (IMHO) both perfectly viable, depending on the context.

True.

But if there are no constraints due to implementing interfaces,
then I will still suggest propagating up.

If there are constraints, then does the best possible.

Arne
 
A

Abu Yahya

The purely object-oriented solution is as follows:

calculate( expression, success, failure );

. »calculate« does the calculation based on the object
»expression« and then calls the »acceptResult« method of the
object »success« if the calculation was a success or the
»acceptFailure« method of the object »failure« if the
calculation failed.

I can't see how this solution could fit in the getResult() method the OP
has written.
 
S

Stefan Ram

Abu Yahya said:
I can't see how this solution could fit in the getResult()
method the OP has written.

Yes, it does not fit in this. I should have written:

»A purely object-oriented solution /would be/ as follows«.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top