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

Unreachable statement

 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. public class MyProgram {
2. public static void throwit() {
3. throw new RuntimeException();
4. }
5. public static void main(String args[]){
6. try {
7. System.out.println("Hello world ");
8. throwit();
9. System.out.println("Done with try block "); //9
10. }
11. finally {
12. System.out.println("Finally executing ");
13. }
14. }

O/p:
The program will print Hello world, then will print Finally executing, then
will print that a RuntimeException has occurred.

My question is:
The method throwIt() explicitly throws a RuntimeException. Then, isn't the statement on line 9 unreachable?
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "conservative flow analysis" used to detect unreachable statements is defined in Java Langauge Specifications 14.20. It does not require examining the code of called methods as part of the analysis of the calling program.

See:
http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#237365
 
Kedar Dravid
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now, consider the following code:
class tr
{
public static void main(String[] args)
{
doStuff();
}
static boolean doStuff()
{
for (int x = 0; x < 3; x++)
{
System.out.println("in for loop");
return true;
}
return true; //1
}
}

O/p:
in for loop
According to me, the statement return true inside the for loop causes the loop to end abuptly. In any case, the line labelled 1 won't be executed, since the for loop will always be executed and terminate on the first iteration.
Isn't line 1 unreachable in that case?
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


According to me, the statement return true inside the for loop causes the loop to end abuptly. In any case, the line labelled 1 won't be executed, since the for loop will always be executed and terminate on the first iteration.


Ur statement accepted, if i were the compiler i would have accepted it and would have displayed a compilation error.
But unfortunately i am not a compiler, compiler is JVM and it doesnt know what's the condition inside the for loop. It supposes that, what if the for loop is like this:

for (int x = 0; x !=0 ; x++)

Then ur last statement gets to execute, compiler is not sure what the condition is going to be, thats why it doesnt throw a compilation error.

But remember one thing, its not the case if ur variable i is a compile time constant
Now look at all these examples:

At 1 u get a compilation error.



But,

So from above examples u can make out that the compile time constants, make the difference here.
For more info read the jls document link given by Mike.
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




You will get compile time error uninitialized local variable z . although I know it is typo .

Why no compile error , because compiler think in all perspective , if if condition will not execute then who will initialize z ...

But, my doubt is why compiler is not able to get this that if condition will not execute whatever is the case . It should give compile time error - unreacable statement .
[ March 22, 2005: Message edited by: rathi ji ]
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You will get compile time error uninitialized local variable z . although I know it is typo .


Sorry that was indeed a typo. Correct one is here:

 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats is why I asked that question because ,

In the fiollowing case , compiler is able to determine that z will initialize .


But in this case , why compiler is not able to determine that z=5 will not execute in any case , why not giving error .
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rathi,
U are correct.
This one very well gives compilation error
as i told in my previous post that the code was not correct.

Explanation is quite obvious and same as u gave.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Animesh ,
I am not concerning about "uninitialized local variable" error . I am concerning with "unreachable statement" errror ...

Hope you got the question .
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks rathi,
was not able to understand ur question,
That made me read the spedific part of jls link
from JLS,


The actual rules for the if statement are as follows:

ACTUAL: An if-then statement can complete normally iff it is reachable. The then-statement is reachable iff the if-then statement is reachable.
ACTUAL: An if-then-else statement can complete normally iff the then-statement can complete normally or the else-statement can complete normally. The then-statement is reachable iff the if-then-else statement is reachable. The else-statement is reachable iff the if-then-else statement is reachable. As an example, the following statement results in a compile-time error:

while (false) { x=3; }

because the statement x=3; is not reachable; but the superficially similar case:

if (false) { x=3; }


From the above statements for an if statement:
"The then-statement is reachable iff the if-then statement is reachable".
So that gives ur answer but remember its not the same for While statement.
Got it.
for while it says:


A while statement can complete normally iff at least one of the following is true:
The while statement is reachable and the condition expression is not a constant expression with value true.
There is a reachable break statement that exits the while statement.



Got it buddy
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Animesh ,
I didn't get this JLS .
Can you put this in simple word ...

Thanks .
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately, the JLS definition of an unreachable statement is not very simple.

If you study this link carefully, you will see what the Java compiler does and see a few comments about why it works that way.
http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#237365

The choice of what kinds of unreachable statements are flagged by the compiler is generally based on what can be easily detected by scanning the abstract syntax tree, the internal form of a Java program from which the compiler generates byte code.

Please remember that the Java SDK hides most of the complexity so that we can use Java without having to understand how it works. This is a kind of encapsulation. But the complexity is still there. Even when the code in the java and javax source libraries looks simple, it was designed with some subtle issues in mind like locality of memory reference and favoring compiler optimization techniques. Some of the really important code was written in languages like C in a hardware-dependent manner so that you and I can write efficient programs in platform-independent Java.

Another case of necessary complexity is the order of evauation of expressions. Java operands are evaluated from left to right, but the grouping (implied parentheses) of operands with operators is based on operator precedence and associativity. Normally, only precedence matters but operands with side effects like i++ and m(i) can lead to some challenging mock exam questions. Luckily, I don't think the real SCJP questions involving the increment and decrement operators are that bad. You'll notice that the authors of the JLS have refused, despite many requests, to publish an operator precedence table, because these tables just don't tell the whole story.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How often do we get questions related to unreachable code in the main certification exam? Its good to know how the internals work but as mike pointed out it is something that is enscapsulated, which unfortunately is not the case while taking the exam as you need to think like a compiler
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rathi,
Its bit complex to understand but i have tried putting it in simple terms to u. Lets see if i succeed.
1) Consider the while loop first:
According to JLS


The contained statement is reachable iff the while statement is reachable and the condition expression is not a constant expression whose value is false.


here the contained statement refers to the statement inside while loop. So the contained statement is reachable if and only if while statement is reachable and the condition expression is
1) not a constant expression having its value false
2) a constant expression having its value true
3) not a constant expression having its value true

Its unreachable only when the condition expression is
1) a constant expression having value false.


Hope its clear to u regarding While statement.
Now lts go on with the if-then-else,
remember dont get confused with the statement if-then-else .
It simply says,

Ok about if-then the JLS says:


The then-statement is reachable iff the if-then statement is reachable


U can see the differences in while and if.
If statement doesnt have any requirement on the Condition statement . It just says, the then-statement is reachable if and only if the if-then statement is reachable.
So if the above code snippets are changed to:


ALL OF THEM ARE REACHABLE

Hope now its clear to u.
Do ask me doubts if u r still getting confused.

Thanks
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont think so such questions ever come in the exam. These are quite complex to understand.
So lets not worry about such questions.
reply
    Bookmark Topic Watch Topic
  • New Topic