Tushar Goel wrote:Because finally have higher precedence. It will executed at any case so whatever written in to finally be executed. So that's why you see return in finally is executed.
There is only 1 case in which finally will not be executed that's when program itself terminated before running reaching finally.
Mohan Mehra wrote:what about the return in catch?
Mohan Mehra wrote:what about the return in catch?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Paweł Baczyński wrote:
Mohan Mehra wrote:what about the return in catch?
It does not matter. If you have a return statement in finally block, this return always is executed (unless the application is closed before it reaches this return statement).
Mohan Mehra wrote:ok. but i want to know whether the return in catch runs or no.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Campbell Ritchie wrote:Actually there is a specified order for execution: it is in the Java Language Specification (JLS). It is one of the JLS' less easy to read parts.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Mike. J. Thompson wrote:I do think this is more than just intellectual curiosity. It's important to know whether the finally block runs before or after the return statement in the try or catch block as the answer to that question can completely change the behaviour of the program.
As Pawel's example neatly shows, the expressions in the return statements in the try and catch blocks do get evaluated before the finally block runs, so any side effects of those expressions will be seen.
Campbell Ritchie wrote:Of course, you need to know what completing normally and completing abruptly mean. They are defined in the same chapter of the JLS and abrupt completion appears to mean all things occurring which transfer control away. That includes return statements.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Mike. J. Thompson wrote:I do think this is more than just intellectual curiosity. It's important to know whether the finally block runs before or after the return statement in the try or catch block as the answer to that question can completely change the behaviour of the program.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
I love a good mentalist. And so does this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|