float f = parseFloat("1.5");
System.out.println(f);
We end up with "1.5", and NOT with "10.0" (the finally is executed at the end, but it seems that execution returns to the catch block). Could you please explain, how this happening?
If you don't have the return in finally, then the return value that was computed at the end of try block is used. This is as per Section 14.17 of JLS.
A return statement with an Expression attempts to transfer control to the invoker of the method that contains it; the value of the Expression becomes the value of the method invocation.
So even if you change the variable, it does not affect the return value, because it has been decided.
If you have a return in finally, then that changes the return value itself.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Well, this code will not compile because sb is not accessible in finally. In any case, please try running it and see what happens. If you don't understand the output, let me know.
If you like our products and services, please help us by posting your review here.
It is certainly not unreachable. The return statement in try and catch (if there is an exception) does get evaluated. It is just that the return value is not returned to the caller. You can put a method call in a return statement and verify that it does get executed. Something like this:
If I uncomment the line in try{} I get an unreachable code error from Eclipse. Commented it prints from catch and from finally. Seems to not make a difference if I make the throw conditional.
My only point is understanding what is going on. I asked why if there's a return in finally is a return elsewhere not unreachable code. You said the return in the try is evaluated and to try a print statement to show it. I tried that but it tells me it's unreachable code.
Your original question was, "If finally has a return, why isn't the return in try and the return in catch unreachable code?" For that, I said a return is try is executed even if there is a return in finally.
I didn't say return in try is always reachable! Return is not a special statement in this respect. Just like any other piece of code, there could be a million examples where return in try is unreachable. But they have nothing to do with having a return in finally!
In your new example, the return in try is unreachable because of the throw statement. Its unreachability has no relation to the return in finally, which is what I was referring to earlier.
If you like our products and services, please help us by posting your review here.
Since there is no code after the finally block, there is no unreachable error raised.
Given the above statement, can one conclude that (at least on this exam), if there is no more code after a try/catch/finally block with return; in every block, the question is not focused on unreachable code error?
Yes, that sounds correct but as I mentioned to you earlier, it is not possible to answer your query with 100% certainty because it is too vague. I may not be able to think of a case where this doesn't hold true at this moment but someone else may. So, if there is no code associated with your situation, the answer can only be speculative.
I would not suggest you to form such rules in your head. Instead, focus on analyzing the given code on a case by case basis.
If you like our products and services, please help us by posting your review here.
Of course you are correct. I think the reason my reasoning has become somewhat warped for the questions on the tests, is like passing the DMV written test which is for driving in a lab not reality of the road, I'm trying to rethink how to assess a questions on the real exam