Page 1 of 1

About Question enthuware.ocajp.i.v7.2.950 :

Posted: Fri Mar 29, 2013 8:21 am
by danail
Hello,

TRY block is executed, then FINALLY block is executed.
So if we remove the "return" statement from "finally" like this:

Code: Select all

public static float parseFloat(String s) {
	float f = 0.0f;
	try {
		f = Float.valueOf(s).floatValue();
		return f;
	} catch (NumberFormatException nfe) {
		f = Float.NaN;
		return f;
	} finally {
		f = 10.0f;
		// return f; // this is removed
	}
}
and call the method like this:

Code: Select all

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?

Thank you!

Re: About Question enthuware.ocajp.i.v7.2.950 :

Posted: Fri Mar 29, 2013 11:46 am
by admin
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.

Re: About Question enthuware.ocajp.i.v7.2.950 :

Posted: Sun Jan 04, 2015 9:52 pm
by coder007
admin wrote: So even if you change the variable, it does not affect the return value, because it has been decided.
If we work with reference variable, changes made in finally block will be applied to the object returned by catch's return statement?

Re: About Question enthuware.ocajp.i.v7.2.950 :

Posted: Sun Jan 04, 2015 10:27 pm
by admin
Not sure what you mean. Please put it in code so that I can understand.
-Paul.

Re: About Question enthuware.ocajp.i.v7.2.950 :

Posted: Thu Jan 08, 2015 11:39 pm
by coder007
For example:

Code: Select all

try {....
     throw new Exception(); }
catch (Exception e) { 
         StringBuilder sb = new StringBuilder("X");
         return sb; }
finally {
sb.append("Y");
}
What is sb value, X or XY ?

Re: About Question enthuware.ocajp.i.v7.2.950 :

Posted: Fri Jan 09, 2015 2:36 am
by admin
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.

Re: About Question enthuware.ocajp.i.v7.2.950 :

Posted: Wed Mar 04, 2015 9:06 pm
by __Bill
If finally has a return, why isn't the return in try and the return in catch unreachable code?

Re: About Question enthuware.ocajp.i.v7.2.950 :

Posted: Wed Mar 04, 2015 9:12 pm
by admin
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:

Code: Select all

try{
  return m1(); //put a print statement in m1.
}finally{
  return 2;
}

Re: About Question enthuware.ocajp.i.v7.2.950 :

Posted: Tue Mar 10, 2015 2:53 pm
by __Bill
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.

Code: Select all

class tstclass {
public static void main(String[] args){

tstclass t = new tstclass();
t.tsttry();


	String tsttry() {

		try {

			throw new Exception();
			// return(printit("from try"));

		} catch (Exception e) {
			return (printit("from catch"));
		} finally {
			return (printit("from finally"));
		}

	}


	String printit(String msg) {
		System.out.println(msg);
		return "";
	}
}

Re: About Question enthuware.ocajp.i.v7.2.950 :

Posted: Tue Mar 10, 2015 6:06 pm
by admin
Not sure what is your point.

Re: About Question enthuware.ocajp.i.v7.2.950 :

Posted: Tue Mar 10, 2015 7:09 pm
by __Bill
admin wrote:Not sure what is your point.
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.

Re: About Question enthuware.ocajp.i.v7.2.950 :

Posted: Tue Mar 10, 2015 9:39 pm
by admin
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.

Re: About Question enthuware.ocajp.i.v7.2.950 :

Posted: Thu Feb 16, 2017 8:09 am
by AndaRO
TRY block is not executed.

Why danail said that "TRY block is executed"?

Re: About Question enthuware.ocajp.i.v7.2.950 :

Posted: Fri Dec 14, 2018 3:27 pm
by OCAJO1
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?

Re: About Question enthuware.ocajp.i.v7.2.950 :

Posted: Fri Dec 14, 2018 9:09 pm
by admin
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.

Re: About Question enthuware.ocajp.i.v7.2.950 :

Posted: Mon Dec 17, 2018 3:18 pm
by OCAJO1
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 :geek: