• 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

JQ+ Question ID :957639266007

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the answers is to remove 7. My question is what if there is an error caught, or no error, return f; will be called two times because finaaly{} has to be called. I have though return is like exit(0), but it appears not. How come the compiler permit two return calls in the same methods?
Could someone plz explain it for me? Thanks

Question ID :957639266007
What can be done to get the following code compile and run?
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
}
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the value returned in the finally clause is the final value - i.e. result of calling the method.
--- if there is no return statement in finally - then the relevant value of try or catch return statement is taken
--- if there is no catch it takes the value from finally only
--- if there is no finally - then the relevant return statement of either try or catch is taken.
so return is taken only once - i.e. in finally clause - if it exists or the relevant try or catch clauses.
experiment withthe foll code -
class ss1
{
public int parseInt( int s )
{
int f = 10; // 1
try
{
f = f/s; // 2
System.out.println( "in try"+ " " +f) ; // 3
return f;

}
catch(ArithmeticException e)
{
f = 0 ; // 4
System.out.println( "in catch"+ " " +f) ; // 3
return f;

}
finally {
f = 23;
System.out.println("In finally");
System.out.println( "in finally"+ " " +f) ; // 3
return f;


}
//System.out.println( f) ; // 7
}
public static void main(String[] args)
{
ss1 s1 = new ss1();

System.out.println("Calling method in main "+ " "+s1.parseInt( 10 ));
System.out.println("Hello World!");
}
}
 
Chris Ben
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great! the codes are more convincing than anything. Thanks.
So actually, the methods return several times and pick up the last choice. THis makes me surprised because this is what the way 'return' usually works in my impression. In addition, if you write something in finally like:
return f; // 6
return g; //g is initialized at the beginning
you will get error like not reachable statement. SO another question is why the program will not keep going to check another return in the finally block? Are there any rules in Java about it? thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic