Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

exception flow

 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Why this code compiles?

public class Conversion
{
public static void main(String[] args)
{
float d = parseFloat("1");
}
public static float parseFloat(String s)
{
float f = 0.0f; //1
try
{
f = Float.valueOf(s).floatValue(); //2
return f; //3
}
catch (NumberFormatException ex)
{
f= Float.NaN; //4
return f; //5
}
finally
{
return f; //6
}
//return f; //7
}
}
In my opininon the line 6 will never be reached, isn't it?
And the Float.valueOf() doesn't give any other exception that can be ignored by the catch and treated eventually by the finally. Neither Float.floatValue().
I'm quite confused.
Thanks,
..Cristian
 
Cristian Negresco
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I know that finally will always be executed, so the return in finally will erase the first return?, if I'll put:
finally
{
return 2*f;
}
I will check.
..Cristian
 
Cristian Negresco
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think the confusion I've made was between consecutive "return" statements and "unreachable" behaviour.
It seems that it is allowed to have more than one return statements one after another if there is a way to guarantee that they will be exec. and finally is one way to do it.
BTW the q. was related to q 957639266007 form jqpus
..Cristian
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try not to have too many return statements in your code, especially from within a try-catch-finally as it can lead to confusing code, as you have seen.
You are correct, finally is virtually guaranteed to execute, even when you have a return in the try or catch part. The most insidious bugs can be introduced when you have a return in you try/catch and also have a return in the finally. Don't do this in any code that you intend to put into production.


------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
will the line 7 in the above code be executed.
If not then the code should not compile.
If an exception is caught and the finally is executed then does the code after finally execute.
Please could anyone explain.
Thanx
Neha
 
Maybe he went home and went to bed. And took this tiny ad with him:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic