Returning an object

Discussion in 'Java' started by mikew01, Feb 20, 2007.

  1. mikew01

    mikew01 Guest

    Hi, I am after some advice regarding returning an object called for in
    one class and constructed in another.
    The class that constructs the object has around ten methods that if
    successful will all be called during creation of the object however
    each method could throw an exception due to IO errors so I was
    wondering how do you code something like this.

    The calling class currently calls a single method in the creation
    class which in turn makes calls to another method and so on until the
    object is built, this is fine if all goes well but how do you deal
    with an exception.
    If an exception is thrown the object will not be built and the calling
    class will have no object to deal with.

    I guess you can use boolean return values for each method in the
    creation class and return null or another sensible value to the caller
    if something goes wrong but this seems clunky.

    Any advice would be appreciated.

    Thanks,

    M.
     
    mikew01, Feb 20, 2007
    #1
    1. Advertising

  2. mikew01

    Chris Dollin Guest

    mikew01 wrote:

    > Hi, I am after some advice regarding returning an object called for in
    > one class and constructed in another.
    > The class that constructs the object has around ten methods that if
    > successful will all be called during creation of the object however
    > each method could throw an exception due to IO errors so I was
    > wondering how do you code something like this.
    >
    > The calling class currently calls a single method in the creation
    > class which in turn makes calls to another method and so on until the
    > object is built, this is fine if all goes well but how do you deal
    > with an exception.
    > If an exception is thrown the object will not be built and the calling
    > class will have no object to deal with.


    Instead, it will have an exception to deal with (or not). If you
    can do something useful in the way of report or rescue, catch the
    exception and do so. If not, let the exception propagate upwards
    (if necessary wrap it or add a throws declaration).

    > I guess you can use boolean return values for each method in the
    > creation class and return null or another sensible value to the caller
    > if something goes wrong but this seems clunky.


    Not only clunky but unnecessary.

    --
    Chris "electric hedgehog" Dollin
    "Never ask that question!" Ambassador Kosh, /Babylon 5/
     
    Chris Dollin, Feb 20, 2007
    #2
    1. Advertising

  3. mikew01

    Eric Sosman Guest

    mikew01 wrote:
    > Hi, I am after some advice regarding returning an object called for in
    > one class and constructed in another.
    > The class that constructs the object has around ten methods that if
    > successful will all be called during creation of the object however
    > each method could throw an exception due to IO errors so I was
    > wondering how do you code something like this.
    >
    > The calling class currently calls a single method in the creation
    > class which in turn makes calls to another method and so on until the
    > object is built, this is fine if all goes well but how do you deal
    > with an exception.
    > If an exception is thrown the object will not be built and the calling
    > class will have no object to deal with.
    >
    > I guess you can use boolean return values for each method in the
    > creation class and return null or another sensible value to the caller
    > if something goes wrong but this seems clunky.


    One approach, as you've outlined, is to return a special
    value to tell the caller the request was unsuccessful:

    Loan loan = bank.getLoan(amount, purpose);
    if (loan == null)
    System.out.println("Buncha tightwads!");
    else
    System.out.println("Party time!");

    Another is to throw an exception and let the caller catch
    it (or pass it outwards to a still higher-level caller):

    try {
    Loan loan = bank.getLoan(amount, purpose);
    System.out.println("Party time!");
    }
    catch (LousyCreditRatingException ex) {
    System.out.println("Buncha tightwads: " + ex);
    }

    Which is better? It depends largely on the expected course
    of events, on whether the sorry-no-object case is considered
    "exceptional" or "routine." In the illustration above, a bank's
    decision to deny a loan request would probably be thought of as
    a "normal" possible outcome, so the first approach would probably
    be favored. But if we change the nouns and verbs a little:

    try {
    License license = driver.demandLicense();
    radio.checkForRapSheet(license.getNumber());
    }
    catch (UnlicensedDriverException ex) {
    System.out.println("Keep your hands in view "
    + "and step out of the car. You in a heap "
    + "of trouble now, kid.");
    }

    .... the sorry-no-object outcome might be considered "abnormal"
    or "exceptional," and the second approach would probably be better.

    There are also combined approaches, using special values for
    "normal failures" and exceptions for "weird cases:"

    try {
    Loan loan = bank.getLoan(amount, purpose);
    if (loan == null)
    System.out.println("Buncha tightwads!");
    else
    System.out.println("Party time!");
    }
    catch (BankWentBrokeException ex) {
    System.out.println("Bank failed! " + ex);
    }

    The long and short of all this is that there is no "one best
    way" that fits all situations equally well. You need to exercise
    some judgment as to what cases are "normal" and "exceptional,"
    and choose accordingly.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Feb 20, 2007
    #3
  4. mikew01 wrote:
    > Hi, I am after some advice regarding returning an object called for in
    > one class and constructed in another.
    > The class that constructs the object has around ten methods that if
    > successful will all be called during creation of the object however
    > each method could throw an exception due to IO errors so I was
    > wondering how do you code something like this.
    >
    > The calling class currently calls a single method in the creation
    > class which in turn makes calls to another method and so on until the
    > object is built, this is fine if all goes well but how do you deal
    > with an exception.
    > If an exception is thrown the object will not be built and the calling
    > class will have no object to deal with.
    >
    > I guess you can use boolean return values for each method in the
    > creation class and return null or another sensible value to the caller
    > if something goes wrong but this seems clunky.


    Why not throw an exception to the class that wanted the object if the
    object cannot be built?

    Look closely at the inter-class interface. Does the caller know about
    the IO activities? If so, it may make sense to just let the IO
    exceptions flow back to the caller.

    In other cases, the caller does not know or care about IO. Then you
    could define an exception of your own that explains the failure to
    create the object in terms that make sense for that interface. You can
    pass the IO exception to the Exception constructor so that no
    information is destroyed.

    The caller's code becomes:

    try{
    SomeType o = interfaceMethod.result();
    // do things with o
    }catch(YourExceptionWhatever e){
    // deal with not having o.
    }

    Patricia
     
    Patricia Shanahan, Feb 20, 2007
    #4
  5. mikew01

    Oliver Wong Guest

    "mikew01" <> wrote in message
    news:...
    > Hi, I am after some advice regarding returning an object called for in
    > one class and constructed in another.
    > The class that constructs the object has around ten methods that if
    > successful will all be called during creation of the object however
    > each method could throw an exception due to IO errors so I was
    > wondering how do you code something like this.



    In addition to the other advice you've received here, you may want to
    take a look at the Builder design pattern for inspiration:
    http://en.wikipedia.org/wiki/Builder_pattern

    - Oliver
     
    Oliver Wong, Feb 20, 2007
    #5
    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. Karuppasamy

    WebService – Returning an Object

    Karuppasamy, Jan 15, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    546
    Karuppasamy
    Jan 15, 2004
  2. Developer
    Replies:
    0
    Views:
    502
    Developer
    Aug 11, 2003
  3. Toby Mathews

    Returning my object(s) from web service

    Toby Mathews, Oct 28, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,126
    Toby Mathews
    Oct 28, 2003
  4. Replies:
    11
    Views:
    692
    Christos Georgiou
    May 2, 2006
  5. Jean-Michel Pichavant

    Mocked object returning another Mocked object

    Jean-Michel Pichavant, Jul 13, 2012, in forum: Python
    Replies:
    1
    Views:
    217
    Ulrich Eckhardt
    Jul 13, 2012
Loading...

Share This Page