Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > values for fields in failure/exception

Reply
Thread Tools

values for fields in failure/exception

 
 
mark jason
Guest
Posts: n/a
 
      12-09-2010
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
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      12-09-2010
On Dec 9, 12:28*pm, mark jason <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      12-10-2010
On 09-12-2010 12:28, mark jason wrote:
> 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
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      12-10-2010
On 12/9/2010 7:38 PM, Arne Vajhøj wrote:
> On 09-12-2010 12:28, mark jason wrote:
>> 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.


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.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      12-10-2010
mark jason <(E-Mail Removed)> writes:
>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 */ }

 
Reply With Quote
 
Jean-Baptiste Nizet
Guest
Posts: n/a
 
      12-10-2010
On 9 déc, 18:28, mark jason <(E-Mail Removed)> 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?
>


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.

> public MyResult getResult() {
> * * MyResult res = null;
> * * try{
> * * * * res = calculate();
>
> * * }catch(SomeException e) {
> * * * * return new MyResult ( false, Double.NaN, "", e.getMessage() );
>
> * * }
> * * return res;
>
> }
>
> regards,
> mark


 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      12-10-2010
On 09-12-2010 21:47, Eric Sosman wrote:
> On 12/9/2010 7:38 PM, Arne Vajhøj wrote:
>> On 09-12-2010 12:28, mark jason wrote:
>>> 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.

>
> 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
 
Reply With Quote
 
Abu Yahya
Guest
Posts: n/a
 
      12-11-2010
On 12/10/2010 11:20 AM, Stefan Ram wrote:
> mark jason<(E-Mail Removed)> writes:
>> 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.
>


I can't see how this solution could fit in the getResult() method the OP
has written.
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      12-11-2010
Abu Yahya <(E-Mail Removed)> writes:
>On 12/10/2010 11:20 AM, Stefan Ram wrote:
>>The purely object-oriented solution is as follows:

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
epydoc: How to add new fields as the building fields? Cyril.Liu Python 0 12-02-2008 05:01 AM
print struct fields and its member structs' fields recursively, generically call_me_anything C++ 4 09-30-2007 10:12 PM
Newbie - Reading a file with delimited fields, storing first two fields in a hash AMT2K5 Perl Misc 1 11-08-2005 01:06 AM
how to copy from fields to other fields middletree ASP General 1 11-05-2003 06:24 PM
Netscape hidden fields - array - multiple fields with same name mark.reichman@rl.af.mil Javascript 0 07-17-2003 03:05 PM



Advertisments