Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Returning an object

Reply
Thread Tools

Returning an object

 
 
mikew01
Guest
Posts: n/a
 
      02-20-2007
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.

 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      02-20-2007
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/

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      02-20-2007
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid

 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      02-20-2007
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
 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      02-20-2007

"mikew01" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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


 
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
Mocked object returning another Mocked object Jean-Michel Pichavant Python 1 07-13-2012 10:37 AM
returning none when it should be returning a list? randomtalk@gmail.com Python 11 05-02-2006 10:26 AM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
WebService – Returning an Object Karuppasamy ASP .Net 0 01-15-2004 04:38 PM
Vb6 object returning ADO Recordset - Error in .NET Developer ASP .Net 0 08-11-2003 03:27 PM



Advertisments