wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception Handling" Watch "Exception Handling" New topic
Author

Exception Handling

Arun Pai
Ranch Hand

Joined: Mar 11, 2002
Posts: 143
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 ;
}
Why this code from JQ+ does not compile?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
What happens if you compile the code???

The return statement after the finally block is not reachable because there already is a return statement in the try and another one in the catch block. So whatever happens, one of those two will be executed and the one outside is unreachable.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Arun Pai
Ranch Hand

Joined: Mar 11, 2002
Posts: 143
Thanks Val. Now I am clear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception Handling
 
Similar Threads
JQ+ and confusion with try/catch/finally
statement not reached
unreachable code in 'finally'
why is finally not being called?
Unreachable Code in try/catch ? why????