programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Junilu Lacar
• Liutauras Vilda
Sheriffs:
• Paul Clapham
• Jeanne Boyarsky
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Piet Souris
• Carey Brown
Bartenders:
• Jesse Duncan
• Frits Walraven
• Mikalai Zaikin

# return

Ranch Hand
Posts: 333
• Number of slices to send:
Optional 'thank-you' note:
Consider the following example from JQPlus:
public float parseFloat(String s)
{
float f = 0.0f;
try
{
f = Float.valueOf(s).floatValue();
return f ;
}
catch(NumberFormatException nfe)
{
System.out.println("Invalid input " + s);
f = Float.NaN ;
return f;
}
finally { System.out.println("finally"); }
return f ;
}

Which of the following statements about the above method are true?

a If input: "0.1" then it will return 0.1 and print finally.

b If input: "0x.1" then it will return Float.Nan and print Invalid Input 0x.1and finally.

c If input: "1" then it will return 1 and print finally.

d If input: "0x1" then it will return 0.0 and print Invalid Input 0x1 and finally.

e The code will not compile.

Explanation given:.
Note that the return statement after finally block is unreachable. Otherwise, choices 1, 2, 3 are valid.
I can't understand why/how the return statement is unreachable.
Also, assuming it is unreachable, shouldn't choice 3(c) print 1.0 and not 1?

Ranch Hand
Posts: 7729
• Number of slices to send:
Optional 'thank-you' note:
Where is it printing 1?

Ranch Hand
Posts: 1272
• Number of slices to send:
Optional 'thank-you' note:
If a NumberFormatException is not thrown, the finally clause will execute, then the first return statement, so the last return statement is not reached.

If a NumberFormatException is thrown, the catch clause is executed but the return action is delayed, then the finally clause is executed, then the delayed return action occurs. The final return statement is still not executed.
[ March 11, 2005: Message edited by: Mike Gershman ]

Kedar Dravid
Ranch Hand
Posts: 333
• Number of slices to send:
Optional 'thank-you' note:
Thanks, Mike!

 pie. tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton