• 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

Do the return statement prevent Exception?

 
Ranch Hand
Posts: 221
27
IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following question is from Enthuware Test 2 (Q 71) and I make some changes to test it:
Output:

try_1: 0.0
try_2: 5.0
finally
5.0

try_1: 0.0
catch_1: 0.0
catch_2: NaN
finally
NaN

try_1: 0.0
finally
0.0


The outputs of line 1 and line 2 are clear, it is a bit confused the output of line 3. I excepted JVM throws NullPointerException, but it didn’t happen. If it would be added the extra catch(NullPointerException ) statement, the exception is caught or If I put comment the last return statement (in finally block) JVM throws NullPointerException. Do the return statement prevent Exception?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main problem in the parseFloat method is the finally block: it has a return statement so it doesn't end normally. Having a return statement in the finally block is allowed by the JLS but it's considered to be a (very) bad practice. Because strange things will happen (as you noticed). If there is a return statement within the finally block, it will trump any other return from the regular block. That is, the following code will print false And the same thing happens with an uncaught exception: because of the return statement, the uncaught exception is discarded and forgotten. Here you'll find the appropriate section of the JLS describing this behavior (it's even in bold ).

Hope it helps!
Kind regards,
Roel

PS. I really like your question and the effort you made to create a quality post: a nice code snippet (correct indentation, nothing more but appropriate code,...), a few different scenarios which you used to test your knowledge and a great question. Have a cow!
 
Mushfiq Mammadov
Ranch Hand
Posts: 221
27
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:The main problem in the parseFloat method is the finally block: it has a return statement so it doesn't end normally. Having a return statement in the finally block is allowed by the JLS but it's considered to be a (very) bad practice. Because strange things will happen (as you noticed). If there is a return statement within the finally block, it will trump any other return from the regular block.


I am totally agree with the underlined part. After first execution I couldn't understand what happened. Then I debug the code step by step and doubt finally block. Although hardly reading everything is written obvious in JLS. Thank you for helpful reply, Roel! And thanks a cow
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mushfiq Mammadov wrote:I am totally agree with the underlined part.


That underlined part might not help you on the actual exam, although I wonder if the question bank has this kind of questions.
 
Mushfiq Mammadov
Ranch Hand
Posts: 221
27
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:That underlined part might not help you on the actual exam, although I wonder if the question bank has this kind of questions.


In really there is a few difference between question bank question and this question. The original question is regarding unreachable or missing return statement. I have made change for testing this code.
 
For my next trick, I'll need the help of a tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic