• Post Reply Bookmark Topic Watch Topic
  • New Topic
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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

statement not reached

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class x {
public float parseFloat(String s)
{
float f = 0.0f;
try
{
f = Float.valueOf(s).floatValue();
return f;
}
catch(NumberFormatException f)
{
System.out.println("Invalid input" + s);
f1 = Float.NAN;
return f1;
}
finally{ System.out.println("finally"); }
return f;
}
}
This code doesnot compile saying that return statement not reached. My question is why does the compiler gives this error. What is use behind it.
If this code doesnot generate any exception then it can be reached at the last return statement of this code.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that the bracket after the println in finally is backwards, which effectively ends the finally block before the return happens. Therefore you have a return statement outside of any method and the compiler complains.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi shabbir
there were a few more errors so here is the compileable code and the return f; statement should be in the finally clause as the compiler sees the return statement after the finally clause even though u've put the same return statement in the try block. so the compiler sort expects an exception to occur because u've put in try catch and finally blocks but if an exception does occur the finally block shoudn't be run as u have a return statement in the catch block, but if u didn't have the return statement in the catch block only the finally block will be run but the return was after the finally block.
hope this is clear
here's the correct code:
public class tester{
public float parseFloat(String s){
float f = 0.0f,f1=0.0f;
try{
f = Float.valueOf(s).floatValue();
return f;
}
catch(NumberFormatException fex){
System.out.println("Invalid input" + s);
f1 = Float.NaN;
return f1;
}
finally{ System.out.println("finally"); return f;}
}
}
 
shabbir zakir
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi!
Thanks lee & cindy for your explanations. It will more clear if someone gives more of this type of error prone codes. Thanks in advance
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shabbir,
The main reason the 'return' stmt in the main()method won't be reached is that, the control will be returned back(exception or no exception) to the calling method because of your 'return' stmts in both the try and catch blocks. And of course before either return control goes to the finally block. Comment out either one of these 'return' stmts and ur code should work. And as Lee pointed out, there were other errors too. Here is the code-
public class x {
public float parseFloat(String s)
{
float f = 0.0f,f1 = 0.0f;
try
{
f = Float.valueOf(s).floatValue();
//return f;
}
catch(NumberFormatException fe)
{
System.out.println("Invalid input" + s);
f1 = Float.NaN;
return f1;
}
finally{ System.out.println("finally"); }
return f;
}
}
 
This tiny ad is suggesting that maybe she should go play in traffic.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic