• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help needed in identifying unreachable code

 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am finding it difficult to identify, unreachable code, does any one have simple, straight forward rule to identify it within a short time frame? I find hard to understand JLS to be frank.

Sample 1:

In this code I chose, compilation fails option, not because of while(true), but thought "break Outer;" will never be able to reach.

Whereas following, simple code, of course wrapped in a complex program, will fail to compile because of unreachable code!!!

Sample 2:

I am writing this here because, answer to this thread here, has helped me a lot, and I am finding it better to answer complex looping code. Hope you will guide me, to resolve my this disability as well.

With great respect,
Prathima
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathima,
The first example has break statements in it so there is the possibility of the loop ending. The compiler doesn't check whether it actually does or not, so you don't have to go into so much detail about tracing it to find out if there is unreachable code. That said, you probably do have to trace it on an exam question to determine what the output is. But the answer would be infinite loop and not unreachable code.

The second example has a loop that is always true and no possibility of ending. Any code after it in th emethod would be unreachable.

Hope this helps!
 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Prathima,
The first example has break statements in it so there is the possibility of the loop ending.




Hi Jeanne,

Thanks for the reply, and I am sorry if I have understood your reply wrongly,



This modified code here, also has break statement, but its an unreachable code.


But the answer would be infinite loop and not unreachable code.


The out put of the original code was, 2.

With regards,
Prathima
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the revised example, the break only breaks out of the inner loop. There is still no possibility of the outer while loop ending.
 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank for the reply Jeanne,

Do you mean to say that, as long as the loop gets broken, there is no chance of unreachable code? And when it fails remaining code will be unreachable?

Thanks for the help,
Prathima
 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Can any one explain me why the below code is not unreachable?



Regards,
Prathima
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.21
To shorten the description of the rules, the customary abbreviation "iff" is used to mean "if and only if."


A try statement can complete normally iff both of the following are true:
The try block can complete normally or any catch block can complete normally.
If the try statement has a finally block, then the finally block can complete normally.



If a finally block is present, it is reachable iff the try statement is reachable.

so there is no requirements that try block must complete normally. (for make finally reachable)

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic