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

Moderator: admin

Post Reply
danail
Posts: 1
Joined: Fri Mar 29, 2013 8:12 am
Contact:

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

Post by danail » Fri Mar 29, 2013 8:21 am

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!

admin
Site Admin
Posts: 8296
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Fri Mar 29, 2013 11:46 am

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.

coder007
Posts: 25
Joined: Wed Dec 17, 2014 9:29 pm
Contact:

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

Post by coder007 » Sun Jan 04, 2015 9:52 pm

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?

admin
Site Admin
Posts: 8296
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sun Jan 04, 2015 10:27 pm

Not sure what you mean. Please put it in code so that I can understand.
-Paul.
If you like our products and services, please help us by posting your review here.

coder007
Posts: 25
Joined: Wed Dec 17, 2014 9:29 pm
Contact:

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

Post by coder007 » Thu Jan 08, 2015 11:39 pm

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 ?

admin
Site Admin
Posts: 8296
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Fri Jan 09, 2015 2:36 am

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.

__Bill
Posts: 25
Joined: Thu Mar 27, 2014 11:35 am
Contact:

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

Post by __Bill » Wed Mar 04, 2015 9:06 pm

If finally has a return, why isn't the return in try and the return in catch unreachable code?

admin
Site Admin
Posts: 8296
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Wed Mar 04, 2015 9:12 pm

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;
}
If you like our products and services, please help us by posting your review here.

__Bill
Posts: 25
Joined: Thu Mar 27, 2014 11:35 am
Contact:

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

Post by __Bill » Tue Mar 10, 2015 2:53 pm

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 "";
	}
}

admin
Site Admin
Posts: 8296
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Tue Mar 10, 2015 6:06 pm

Not sure what is your point.
If you like our products and services, please help us by posting your review here.

__Bill
Posts: 25
Joined: Thu Mar 27, 2014 11:35 am
Contact:

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

Post by __Bill » Tue Mar 10, 2015 7:09 pm

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.

admin
Site Admin
Posts: 8296
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Tue Mar 10, 2015 9:39 pm

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.

AndaRO
Posts: 31
Joined: Wed Feb 08, 2017 5:42 pm
Contact:

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

Post by AndaRO » Thu Feb 16, 2017 8:09 am

TRY block is not executed.

Why danail said that "TRY block is executed"?

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post by OCAJO1 » Fri Dec 14, 2018 3:27 pm

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?

admin
Site Admin
Posts: 8296
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Fri Dec 14, 2018 9:09 pm

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.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post by OCAJO1 » Mon Dec 17, 2018 3:18 pm

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:

Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests