Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

Returning an object

Thread view: 
mikew01 - 20 Feb 2007 14:07 GMT
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.
Chris Dollin - 20 Feb 2007 14:37 GMT
> Hi, I am after some advice regarding returning an object called for in
> one class and constructed in another.
[quoted text clipped - 9 lines]
> 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.

Signature

Chris "electric hedgehog" Dollin
"Never ask that question!" Ambassador Kosh, /Babylon 5/

Eric Sosman - 20 Feb 2007 14:38 GMT
> Hi, I am after some advice regarding returning an object called for in
> one class and constructed in another.
[quoted text clipped - 13 lines]
> 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.

Signature

Eric Sosman
esosman@acm-dot-org.invalid

Patricia Shanahan - 20 Feb 2007 15:10 GMT
> Hi, I am after some advice regarding returning an object called for in
> one class and constructed in another.
[quoted text clipped - 13 lines]
> 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
Oliver Wong - 20 Feb 2007 16:29 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.