Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Question about unreachable code compiler error

 
Greenhorn
Posts: 22
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, I feel a bit confused by this question from the Whizlabs mock tests for the OCAJP:

I thought this code would throw an unreachable code compiler error because of lines 5 and 6. The if condition on line 6 will never be true because line 5 will always prevent that line from being reached under conditions where it can be true. So the "break Loop1" statement will never be reached. However both the test results and my compiler say this is fine and does not lead to errors. I guess that's because "unreachable code" doesn't apply to statements inside an "if" block, even if the condition can never be true? But I've seen classes fail to compile because of a "while" condition that was never true. I don't understand this difference. What are the reasons? Am I missing something? And are there other exceptions to the "Unreachable Code" error?

I never expected the "junior" exam to be this hard.
 
Bartender
Posts: 2230
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java Language Specification states:

JLS wrote:Except for the special treatment of while, do, and for statements whose condition expression has the constant value true, the values of expressions are not taken into account in the flow analysis.


You think (and know) that the statement will never be reached. But for the compiler it is reachable! The reason is, as stated by JLS, that the compiler does not examine what is written inside if as a condition.

You could even write:and for the compiler the statement would be reachable.

It is not hard. You are just overthinking it.

Of course this is also not super easy. The JLS states that while, do and for loops are treated differently.

This won't compile:
And this will compile:
 
Aline Galea
Greenhorn
Posts: 22
2
IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:The Java Language Specification states:

JLS wrote:Except for the special treatment of while, do, and for statements whose condition expression has the constant value true, the values of expressions are not taken into account in the flow analysis.


You think (and know) that the statement will never be reached. But for the compiler it is reachable! The reason is, as stated by JLS, that the compiler does not examine what is written inside if as a condition.


Thanks a lot for this bit of explanation that I was somehow not able to find on my own. I will do some more tests but I think I got it now.

Paweł Baczyński wrote:It is not hard. You are just overthinking it.


The problem is, there are other aspects on the exam that I was clearly underthinking ^^ Those questions sometimes throw absurd code at me just to see if I know of some rule that is never used by programmers in real life (like yes, apparently handling an error is legal, but apparently you shouldn't, so you'll never see code that does that... except on the exam). I find that difficult (and a bit frustrating at times) (but I am glad that I'm developing a deeper understanding of java).
 
Marshal
Posts: 25676
69
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the exams are full of code fragments which no reasonable programmer would ever write in real life. But if you become a professional programmer there's a good chance that you will find yourself working on code which was written by un-reasonable programmers, and you'll have to figure out what it's supposed to do. So if you look at it that way, this exam is testing your ability to do that.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Aline Galea wrote:I guess that's because "unreachable code" doesn't apply to statements inside an "if" block, even if the condition can never be true? But I've seen classes fail to compile because of a "while" condition that was never true. I don't understand this difference. What are the reasons? Am I missing something? And are there other exceptions to the "Unreachable Code" error?


Luckily for you, the "unreachable code" topic is one of the more popular ones in this forum. So using the search function you'll find plenty of topics about "unreachable code". Here is a list which could probably be added to my certification book
  • return value for method.
  • Confused point about infinite loop
  • continue and break in a loop would generate compilation error?
  • Which of these is unreachable code?
  • Unreachable Code
  • Which is the first line to cause error?
  • Maybe "unreachable code" should be "dead code" in page 70, (Java OCA 8 Programmer I Study Guide)
  • Throwing a second exception question
  • System.exit() and unreachable code
  • good examples of unreachable code?

  • I think you know what to do this weekend

    Hope it helps!
    Kind regards,
    Roel
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
      Bookmark Topic Watch Topic
    • New Topic