Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Do the return statement prevent Exception?

 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic