Putting return() in catch block

J

John Bailo

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?
 
J

Jim Korman

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?

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
 
B

Benji

John said:
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());
}
}
 
S

shawnews

Benji said:
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
 
B

Benji

shawnews said:
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. =)
 
J

John Bailo

shawnews said:
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()?
 
S

steve

shawnews said:
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()?

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

Steve
 
R

Roedy Green

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top