Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Different criteria for some 'unreachable code'? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Different criteria for some Watch "Different criteria for some New topic
Author

Different criteria for some 'unreachable code'?

David Sabate
Greenhorn

Joined: Feb 25, 2013
Posts: 8
Hello all,

I'm trying to understand why in some situations the compiler seems to check if certain code is unreachable or not.
From the below 2 examples the compiler will detect unreachable code in different lines depending of the loop condition:





But this is not the case if we have a conditional statement? Why the compiler does not detect that there is still unreachable code also in both examples?





Thanks in advance,
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2231
    
    7

Welcome to the Ranch.

Given your example, the "while(true)" and "if(true)" lines are exactly the same. They will "always" throw the exception, making the next line b=37 unreachable.

On the contrary the "while(false)" and "if(false)" do the opposite - never throw exception and can assign b=37.

If you did not explicitly use "true" or "false" in the checking, the compiler may allow you to compile. But runtime is another story. Able to compile doesn't mean able to run successfully.



K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

I know this has been asked before on this forum. I think the If-block gets 'preferential treatment'. For years, a common idiom in many languages has been this:



Then, when you want to debug, you change one value in one spot.

With things like log4j, this is really sort of obsolete, but you don't want to change the compiler in a way that can break existing code, nor do you want to annoy 'old school' programmers who have been doing exactly this sort of thing for possibly decades.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

David Sabate wrote:
I'm trying to understand why in some situations the compiler seems to check if certain code is unreachable or not.

But this is not the case if we have a conditional statement? Why the compiler does not detect that there is still unreachable code also in both examples?


Interestingly, the compiler does detect the unreachable code for the condition statement. It is just designed to not complain about it....


Why? There is a use-case where unreachable code is actually important. For example, what if you wanted to do this?



basically, you like to do stuff during testing, but once it is all tested, you want to set a flag and all debugging stuff is turned off. The unreachable code checks would have required you to go back and comment out the code.

So, to allow this use-case, the Java language is designed to ignore unreachable code for condition statements.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mauro Mazzucco
Ranch Hand

Joined: Apr 26, 2013
Posts: 32

Its because the assertion mechanism was only available in the java 1.4, before that we used to do that for debugging
Now please use assertion instead
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Different criteria for some 'unreachable code'?
 
Similar Threads
While Loop Mystery...
Why not "Unreachable Code" Error..?
about statement unreachable
while loop problem
Unreachable statement