Hi Paul!
I have a doubt with this exercise because of the return statement and the finally block. I Know that finally block is always executed wether there is or not thrown exception.
So in this code, I commented the line 7:
public float parseFloat( String s ){
float f = 0.0f; // 1
try{
f = Float.valueOf( s ).floatValue(); // 2
return f ; // 3
}
catch(NumberFormatException nfe){
f = Float.NaN ; // 4
return f; // 5
}
finally {
return f; // 6
}
// return f ; // 7
}
My question is:
If a NFE is thrown, is the return statement of the catch block executed?
Because if the finally block is always executed, how is possible to be executed if the catch block returns the control to the method invoked...
Here I am missing something, I need help
Thank you very much Paul
About Question enthuware.ocajp.i.v7.2.1200 :
Moderator: admin
-
- Posts: 66
- Joined: Mon Feb 20, 2017 12:31 pm
- Contact:
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1200 :
The return in catch is executed but the method doesn't end (i.e. the return value is not given back to the caller) until finally is finished. However, since finally also has a return statement, the value of this return statement overwrites the value of the previous return statement. Therefore, the caller gets the value returned by the return statement of finally.
Try this code:
Try this code:
Code: Select all
public class TestClass
{
static int x = 5; //0
public static float parseFloat( String s ){
float f = 0.0f; // 1
try{
f = Float.valueOf( s ).floatValue(); // 2
return f ; // 3
}
catch(NumberFormatException nfe){
f = 20.0f ; // 4
return x--; // 5 Observe that x-- will be executed and x will become 4
}
finally {
return 1000.0f; // 6
}
// return f ; // 7
}
public static void main(String[] args){
System.out.println(parseFloat(args[0])); //prints 1000.0
System.out.println(x); // prints 4 This proves that the return statement in
//catch was executed but value returned was from the finally
}
}
If you like our products and services, please help us by posting your review here.
-
- Posts: 66
- Joined: Mon Feb 20, 2017 12:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1200 :
I understand it now!!
Thanks a lot for the explanation!!
Thanks a lot for the explanation!!
-
- Posts: 23
- Joined: Fri Mar 26, 2021 7:25 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1200 :
why option3 is correct?
If //5 and //6 are removed, there would be a scenario where an exception occurs and the return will never be reached.
option3 is wrong
If //5 and //6 are removed, there would be a scenario where an exception occurs and the return will never be reached.
option3 is wrong
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1200 :
Can you show exactly what scenario can occur when? Please post exact code that you have in mind.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 51 guests