• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About unreachable statements with if

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's already discussed. But it's is not satisfactory... Somebody explain it further! I got it here.

I made some modification there!

 
Anuradha Prasanna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
About the below code,

try {
if(true) //line 1
throw new Exception();
System.out.println("A"); // line 2
} catch(Exception e) {}

According to my understanding, since the if(true) in line 1 is not evaluated at compile time(the expression inside if() is not evaluated at compile time), line 2 is considered reachable at compile time.
But at runtime, line 2 is never reachable.
 
Venkata Kumar
Ranch Hand
Posts: 110
Firefox Browser Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see the following Henry comments in the thread

The failure of the compiler to correctly detect unreachable code, when the "if" condition is involved, is not due to the optimizer. It is actually defined in the JLS to be like that.

In the Java Language Specification, it actually defines what should be done (hypothetical) and what is done (actual) for the "if" statement. The unreachability tests for the "if" statement is purposely weaken, in order to allow for conditional code. This way, you can turn on or off debugging code, and not have the compiler complain the code inside the "if" or "else" block is unreachable.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anuradha Prasanna wrote:About the below code,

try {
if(true) //line 1
throw new Exception();
System.out.println("A"); // line 2
} catch(Exception e) {}

According to my understanding, since the if(true) in line 1 is not evaluated at compile time(the expression inside if() is not evaluated at compile time), line 2 is considered reachable at compile time.
But at runtime, line 2 is never reachable.


Ok Anuradha Prasanna, Then what is the difference between line 6 and line 26 in my coding? Both are compile time constants???
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anybody here???
 
Anuradha Prasanna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At line 6,
if(true) --> the expression inside if() loop(in this case, "true") is not evaluated at compile time.
so the line 9, "System.out.println("A"); is reachable.

At line 26,
if(9<10) --> "9<10" is not evaluated at compile time.
To make this clear,
if you change line 26 as,
if(10<9) l = 10;(assuming l is initialized before), this will compile, but at runtime, "10>9" is evaluated as "false" and "l=10" is not reached.

Hope your doubts are cleared now.
 
Vivek K Singh
Ranch Hand
Posts: 85
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the thread post by Venkat defines it in clear terms "In the Java Language Specification, it actually defines what should be done (hypothetical) and what is done (actual) for the "if" statement.

The unreachability tests for the "if" statement is purposely weaken, in order to allow for conditional code. This way, you can turn on or off debugging code, and not have the compiler complain the code inside the "if" or "else" block is unreachable."

Also look at this:


Here as well else part will never get executed, but the weaken unreachability tests for the "if" statement will not force you to change it showing a compile time error.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic