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 / October 2005

Tip: Looking for answers? Try searching our database.

Putting return() in catch block

Thread view: 
John Bailo - 26 Oct 2005 00:52 GMT
I am writing some try/catch to run various processes in main().

I want the whole program to fail if any step fails, so I thought about
putting a "return" inside of the catch to halt the program.

But, then I thought -- would the finally() execute if I ended execution
from a catch{} in main() method?
Jim Korman - 26 Oct 2005 01:23 GMT
>I am writing some try/catch to run various processes in main().
>
[quoted text clipped - 3 lines]
>But, then I thought -- would the finally() execute if I ended execution
>from a catch{} in main() method?

Whenever this kind question comes to mind write code...

public class ReturnTest {
  public static void main(String [] args) {
     try {
        System.out.println("Inside the try block");
        throw new Throwable();
     }
     catch(Throwable t) {
        System.out.println("Inside the catch");
        return;
     }
     finally {
        System.out.println("Inside the finally");
     }
  }
}

and run it! BTW you might want to try using
System.exit(0) instead of return and see what happens.

Then figure out why it happens that way...Instructive.

Jim
Benji - 26 Oct 2005 02:03 GMT
> I am writing some try/catch to run various processes in main().

> I want the whole program to fail if any step fails, so I thought about
> putting a "return" inside of the catch to halt the program.

> But, then I thought -- would the finally() execute if I ended execution
> from a catch{} in main() method?

finally (as its name implies) always executes last - even if you call
System.exit(0) from within the method, or return a value.
(try running the following code:)

public class Test
{
   public static String test()
   {
       try
       {
           return "expected";
       }
       finally
       {
           return "actually";
       }
   }
   
   public static void main(String[] argv)
   {
       System.out.println(test());
   }
}

Signature

Of making better designs there is no end,
 and much refactoring wearies the body.

shawnews - 26 Oct 2005 02:19 GMT
> finally (as its name implies) always executes last - even if you call
> System.exit(0) from within the method

Not true - 'finally' does not execute if you call
System.exit(0) from within the method
Benji - 26 Oct 2005 02:35 GMT
> Not true - 'finally' does not execute if you call
> System.exit(0) from within the method

Interesting...thanks for pointing that out.  I suppose I should have
tested it before saying so.  =)

Signature

Of making better designs there is no end,
 and much refactoring wearies the body.

John Bailo - 26 Oct 2005 18:51 GMT
> Not true - 'finally' does not execute if you call
> System.exit(0) from within the method

My real question is:

Is this a good design structure?

So, say there are three steps in my application, and I don't want the 2
to execute if the 1st is in error, and so on.

Is it a good idea to do this:

public static void main(String[] args) {

try{ method1() } catch(Exception e) { return; }
try{ method2() } catch(Exception e) { return; }
try{ method3() } catch(Exception e) { return; }

}

Or should I throw some other high level exception wihtin main()?
steve - 28 Oct 2005 22:48 GMT
>> Not true - 'finally' does not execute if you call
>> System.exit(0) from within the method
[quoted text clipped - 17 lines]
>
> Or should I throw some other high level exception wihtin main()?

No it is not a good idea, and no it is not good design structure.

Steve
Roedy Green - 29 Oct 2005 02:19 GMT
>try{ method1() } catch(Exception e) { return; }
>try{ method2() } catch(Exception e) { return; }
>try{ method3() } catch(Exception e) { return; }

Normally you either

1. catch an exception and do something about it, a least report the
problem,

2. catch the exception and possibly rethrow it with a better
explanation

3. let it percolate up to the caller

You never just catch it and ignore it, except EOFException. Even then
you are using it to escape a loop and carry on.  You are not ignoring
it.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Bhagya - 26 Oct 2005 12:02 GMT
Hi,
If u r using return statement inside try catch block it wil
executed finally method, after it will return .

Just puting the statement  using System.exit(0);  instead of retru
statement.

Bhagy

-----------------------------------------------------------------------
IT Interview Questions : http://www.geekinterview.com IT Tutorials and Articles : http://www.geekarticles.com Oracle and Oracle Apps Training : http://www.exforsys.com


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



©2009 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.