• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Which of these is unreachable code?

 
Ranch Hand
Posts: 789
Python C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is the unreachable code? I'm getting results that contradict what I've read I'm supposed to get. Maybe there are conditions I don't understand.


 
Marshal
Posts: 5658
330
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What results did you get? What results were you expecting, and why?
 
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

Guillermo Ishi wrote:Where is the unreachable code? I'm getting results that contradict what I've read I'm supposed to get.


I'm happy to announce the compiler agrees with my initial thought/answer
 
Guillermo Ishi
Ranch Hand
Posts: 789
Python C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:What results did you get? What results were you expecting, and why?



I expected the returns to be unreachable code except the return in finally. My results are 1 and 4 are unreachable. I don't know why 2 is reachable! I've been led to believe 1,2,and 3 should be reachable.


P.S. -- okay the return in try is short circuited by the throw so it's unreachable. I still don't have confidence in what is going on in the snippet though.



 
Roel De Nijs
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

Guillermo Ishi wrote:I've been led to believe 1,2,and 3 should be reachable.


1 can never be reachable, the Exception is always thrown (and thus control flow jumps to the catch block)!

Because 2 and 3 are reachable, 4 can never be reachable. If you would comment the finally block, 4 is still unreachable because of 2. So 4 can only be reachable if you comment both return statements 2 and 3.

Now let's have some fun. If you add an always-true if-statement before the throw-statement, 1 will be reachable (but 4 still is not).

Hope it helps!
Kind regards,
Roel

[edit] fixed typo (thanks Mushfiq Mammadov for reporting)
 
Roel De Nijs
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

Guillermo Ishi wrote:I still don't have confidence in what is going on in the snippet though.


The Exception is thrown, so the code moves to the catch block (2 is returned). But as you know, finally runs (almost) always, so the return value will be 3.

For every branch in a method (with a return ype) you need to have exactly 1 return value. If you have a finally block in your method, this block will run (almost) always. So if the finally block has a return statement (which isn't encouraged, it should always complete normally), this value will always be returned (and that's why 4 is unreachable). If you would comment the return statement in finally, the code still moves to the catch block and 2 is returned (and 4 is still unreachable).

If you would add another catch-block without a return statement (and your code doesn't have a finally block with a return statement), 4 will be reachable. Because that will be the return value for the branch of the catch-block without any return value (even if this catch block is never reached by the code inside the try-block). Illustrated in this example:

Hope it helps!
Kind regards,
Roel
 
Guillermo Ishi
Ranch Hand
Posts: 789
Python C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the rules seem to be if you have a return in a finally block, anything after the finally block is always unreachable. I'm assuming here if some condition isn't met so the try that goes with the finally is never executed, then that is false..

If you do not have a finally block, then 4 is reachable (but not necessarily reached if catch has a return).

If you have a catch and a finally both with returns and an exception was thrown both returns get evaluated but the returned value is the one in finally.

As long as it's logical... None of this can't have a long in a switch block stuff...
 
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

Guillermo Ishi wrote:So the rules seem to be if you have a return in a finally block, anything after the finally block is always unreachable.


It depends! If you have a condition in the finally as in the following code, the return statement after the finally is still reachable.

Guillermo Ishi wrote:I'm assuming here if some condition isn't met so the try that goes with the finally is never executed, then that is false..


Exactly! This code will compile successfully!

Guillermo Ishi wrote:If you do not have a finally block, then 4 is reachable (but not necessarily reached if catch has a return).


Not true! If each catch-block has a return statement, 4 is still unreachable.

Guillermo Ishi wrote:If you have a catch and a finally both with returns and an exception was thrown both returns get evaluated but the returned value is the one in finally.


Spot-on! So if you remove the compiler errors from your code snippet in the OP, the test method returns 3.

Hope it helps!
Kind regards,
Roel
 
Guillermo Ishi
Ranch Hand
Posts: 789
Python C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Not true! If each catch-block has a return statement, 4 is still unreachable. "


It seems to depend on whether the compiler knows it's unreachable without it having to think very hard...

It thinks 4 is reachable here:


but knows it's not reachable here:

 
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

Guillermo Ishi wrote:It seems to depend on whether the compiler knows it's unreachable without it having to think very hard...


The compiler never executes any code! That's why in your 1st snippet it doesn't know that statement will throw a runtime exception.
In the 2nd snippet the compiler knows/sees (without running any code) the exception will always be thrown. So control flow moves to catch block which has a return statement which will always be executed. So 4 will never be reached!
 
Guillermo Ishi
Ranch Hand
Posts: 789
Python C++ Linux
  • 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 compiler never executes any code!



That's a good way to think about this. That will be helpful.
 
straws are for suckers. tiny ads are for attractive people.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic