Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Try/catch/finally  RSS feed

 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a method has a try/catch/finally block and the catch has a return in it, how does this effect flow control?
ie does the finally still get executed, which value is returned (the finally has a return also )
I'd try it myself but I don't have access to a compiler at the moment...
Dave.
 
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The runtime system always executes the statements within the finally block regardless of what happens within the try block.
Hence the value in the finally block would be returned. You can check the following code which gives an output as "finally" even though the execution flow went to catch block also.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In regards to the previous code example:
The VM tries to execute the println statement in the try block, and throws an exception because it is trying to access slot 5 of the array which is out of bounds. The exception is caught by the proceeding try block, but the VM does not execute the catch block's return statement until it executes the code in the finally block. The VM then executes the code in the finally block and exits the method call as it executes the finally block's return statement.
What if the finally block didn't have a return statement? Would the VM execute the code in the finally block then execute the return statement in the catch block?
 
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes... it would...

I'm not sure if this would tally with what actually goes on, but it seems to me that what the return statement is doing is saying, "Set the value that I am going to return to the specified value, and then exit... unless there is something I absolutely, positively need to do before exiting..."

-Nate
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, Nathan. If the catch block has a return statement, the value is set before the finally block executes. So, if you try and set the return variable in the finally block, it will have no effect.


The finally block doesn't always execute though. If the catch block contains a System.exit() call, the program will exit immediately.
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I think the return value in finally is retuned not the return in the catch block.
even i dont have a jdk in my machine right now i cant make sure.
Please try it.
ideally it shud return the return value given in finally
Bye cherry
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Angela,

Sorry... the value in the finally block is returned in the example given. I was answering the question about "if there was no return in finally". Then the value in the catch block would be returned, but any code in the finally block would be run before the method actually returned... if the finally block contains a return statement, then the value specified is returned.

Sorry for any confusion,
-Nate
 
Angela Lamb
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess my previous post wasn't very clear. I wasn't talking about the example code in the second post, I was responding to the observation that the return value is set in the catch block. If you try to reset the variable again in the finally block, it won't have any effect. Of course, if you have another return statement in the finally block, then the return in the catch block won't be executed.

The output for this is 1, so you can see that return value of the catch block was already set before the finally block executed.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I finally got the chance to test the behaviour out and I'm dropping the results here cos they're completely bizarre. I include the information below just to close the issue but note that I am NOT recommending anyone do it this way, quite the opposite. If you use code like this, you may well be an idiot

The difference between methodOne() and the other two is that it handles all exceptions inside itself. The difference between methodTwo() and methodThree() is that they both rethrow an exception but methodTwo() returns from the finally but methodThree() returns after this.
Try it out for your self, but the following occurs:

  • methodOne() will return 'finally' regardless of whether an exception was thrown or not.
  • methodTwo() will always return 'finally' regardless of whether an Exception was thrown or not, and can forget that an Exception was even thrown!
  • methodThree() will propogate the Exception correctly and be caught in the main().

  • OK, so maybe it's a bit overboard but I found the results interesting. I accept that you should never have code anything like this, but the fact that you can get the JVM to 'forget' a return statement or even an Exception is kind of funny.
    Dave
 
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!