values for fields in failure/exception

Discussion in 'Java' started by mark jason, Dec 9, 2010.

  1. mark jason

    mark jason Guest

    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
     
    mark jason, Dec 9, 2010
    #1
    1. Advertising

  2. mark jason

    Lew Guest

    On Dec 9, 12:28 pm, mark jason <> 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
     
    Lew, Dec 9, 2010
    #2
    1. Advertising

  3. mark jason

    Arne Vajhøj Guest

    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
     
    Arne Vajhøj, Dec 10, 2010
    #3
  4. mark jason

    Eric Sosman Guest

    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
    lid
     
    Eric Sosman, Dec 10, 2010
    #4
  5. mark jason

    Stefan Ram Guest

    mark jason <> 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 */ }
     
    Stefan Ram, Dec 10, 2010
    #5
  6. On 9 déc, 18:28, mark jason <> 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
     
    Jean-Baptiste Nizet, Dec 10, 2010
    #6
  7. mark jason

    Arne Vajhøj Guest

    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
     
    Arne Vajhøj, Dec 10, 2010
    #7
  8. mark jason

    Abu Yahya Guest

    On 12/10/2010 11:20 AM, Stefan Ram wrote:
    > mark jason<> 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.
     
    Abu Yahya, Dec 11, 2010
    #8
  9. mark jason

    Stefan Ram Guest

    Abu Yahya <> 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«.
     
    Stefan Ram, Dec 11, 2010
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    0
    Views:
    593
    Ilias Lazaridis
    Feb 1, 2005
  2. call_me_anything
    Replies:
    4
    Views:
    473
    Pete Becker
    Sep 30, 2007
  3. Replies:
    0
    Views:
    495
  4. bnp
    Replies:
    4
    Views:
    335
  5. friend
    Replies:
    1
    Views:
    217
    Thomas 'PointedEars' Lahn
    May 12, 2008
Loading...

Share This Page