File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why is finally not being called? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why is finally not being called?" Watch "why is finally not being called?" New topic
Author

why is finally not being called?

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
Following is a supposedly robust method to parse an input for a float....
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??
1)If input: "0.1" then it will return 0.1 and print finally
2)If input "0x.1" then it will return Float.Nan and print Invalid 0x.1 and finally
3)If input: "1" then it will return 1 and print finally
4)If input :"0x1" then it will return 0.0 and print Invalid 0x1 and finally
5)The code will fail to compile
Ans: 5) It will fail to compile
Can any one explain me why doesnt the code compile?
Sonir
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
Sonir :
Did you try to compile this code ? What error message did the compiler give you? At which line?

You _have_ to get into the habit of actually trying all the code that you have questions about. That is an important factor of how well you will do in the exam.
HTH
sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
Shivaji,
I dont have the full length code with me as I took this from a mock Test.
I just want to know why is finally not being called?
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
It looks as though the last Return, that is after the finally block may be the culprit.
All other blocks such as Try , catch and finally have a return statement in them. So the last return will never be executed. The compiler will complain about that.
HTH
Vivian Chai
Greenhorn

Joined: Sep 10, 2001
Posts: 13
The f is a local variable, it can not be seen outside of parseFloat() block. so it is a compile time error.
Vivian Chai
Greenhorn

Joined: Sep 10, 2001
Posts: 13
Sorry! I did not read the question carefully.
Vivian Chai
Greenhorn

Joined: Sep 10, 2001
Posts: 13
I compiled this code, and Shivaji Marathe is right, the error reported by compiler is:
Statement no reached.
return f;
^
Error compiling parseFloat.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Windy,

The usage of f is not a problem. It's local to the method, and only being used in the method, so there's no problem there.
Sonir, I'd like to follow up with what Shivaji first mentioned...the ONLY way to answer this question is COMPILE THE CODE. This takes all the guesswork out of it!!!
You are never going to really learn this material by just reading a few example methods and asking "what does it do?". You have to experiement with it yourself and make the discovery on your own, so you can really understand what is going on.
Here is a tip on how you can actually compile this, even if all you have is a code snippet. In the example, you have an entire method, so you can treat it as a unit. I'll first create a little shell app (you MUST understand and be able to do this without any kind of reference!!)

This is almost the simplest java program you can write. It doesn't actually *do* anything, but you can compile it and run it!.
Now, take that code example method, and paste it into this shell application
:

Ok, NOW you have this method in an application. You know that it compiled fine before you added this method right? So, if you compile it now, and there's an error, you *know* it's in the method you just added.
Your homework assignment Sonir is to copy the code above, paste it into your IDE, and compile it. Then see what happens....come back and copy the output from the compiler here and I will explain what to do next.


Rob


Rob
SCJP 1.4
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: why is finally not being called?
 
Similar Threads
return
Exception Handling
JQ+ and confusion with try/catch/finally
JQPlus Question
COnfusion About Unreachable Code