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 / July 2006

Tip: Looking for answers? Try searching our database.

handling exception

Thread view: 
mangaldo - 14 Jul 2006 12:21 GMT
Hi i m newbie to java.
can anybody tell me can return statement be used in catch block.
if yes if it is good programming practice..pls explain
Hendrik Maryns - 14 Jul 2006 12:25 GMT
mangaldo schreef:
> Hi i m newbie to java.
> can anybody tell me can return statement be used in catch block.

Yes.

> if yes if it is good programming practice..pls explain

Difficult to say.  Often no, but I can imagine there are cases where it
would be ok.  Give some example code, and I’ll tell you.  But of course,
your professor won’t be satisfied with this answer...

H.
- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
mangaldo - 14 Jul 2006 12:40 GMT
Hi Hendrik,
Thanks a lot for replying..
Here is a case,
I call one method from one of the working thread, and i want it to
return true if successful otherwise false.

pls advice

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 26 lines]
> =MnBa
> -----END PGP SIGNATURE-----
Hendrik Maryns - 14 Jul 2006 13:46 GMT
mangaldo schreef:
> Hi Hendrik,
> Thanks a lot for replying..
> Here is a case,
> I call one method from one of the working thread, and i want it to
> return true if successful otherwise false.

Returning a boolean method for success is not considered good practice,
but I might get reactions on this (google for function vs. procedure).
The way to let know that it didn’t work, is to throw an exception.  A
method ending normally is considered success.

I don’t really see what this has to do with you original question,
please give more concrete example code.

And please do not top-post.

H.

> pls advice
>
[quoted text clipped - 7 lines]
> would be ok.  Give some example code, and I'll tell you.  But of course,
> your professor won't be satisfied with this answer...

- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Paul Davis - 14 Jul 2006 15:45 GMT
Agreed, returning out of a catch block will in most cases be something
to be avoided.
(Most best practices recommend striving for a single return)

It can lead to hard to read code when a finally block is added. The
code in the finally will execute before the return even though it is
located after the return in the source.
try{
  // do risky stuff
}catch( Exception e){
 return false;
} finally {
 // clean up resources
}

I do agree with your other post that instead of trapping the exception
and returning a boolean that he should just let the exception
propogate. Swallowing the exception can lead to hard to debug code.


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.