• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

examlab(question-53)-unreachable statement NAO?

 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

output:3
yes this is because line 1 throw nullpointerexception,so line 2 will no get executed. ok fine
So line 2 should be called a unreachable statement.isn't it?
Also but if we commented out line 3,it says line3 is a unreachable statement gives compiler error.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So line 2 should be called a unreachable statement.isn't it?

No, because it's in a conditional block.

Also but if we commented out line 3,it says line3 is a unreachable statement gives compiler error.

Did you leave throw new ArrayIndexOutOfBoundsException(); uncommented ? If you don't comment it, or if you don't put it in a conditional block like the NPE, the compiler will complain.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 18 is conditional. So it can (and in this case always does) throw an Exception. But because it's conditional lines after that are reachable.
Line 19 is unconditional so the compiler knows it will always throw an Exception so lines after that are unreachable.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:
So line 2 should be called a unreachable statement.isn't it?

No, because it's in a conditional block.

Wouter Oet wrote:
Line 19 is unconditional so the compiler knows it will always throw an Exception so lines after that are unreachable.

line 19 and line 2 are same.
can you guys compare your answers,is line 19(aka line2) is in conditional block(as Christophe Verré says) or is unconditional(as wouter says).
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this :

and this :

and this :

Do you see the difference ?
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got the losing concept.
thanks Sheriff and Mr. Rancher for your reply that can be digested easily.
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, What is this logic/concept/feature called here ? The first statement after if condition is "dead Code". Second statement maybe "Dead Code" or "Unreachable code" as given below..What is happening here ?
 
Henry Wong
author
Marshal
Pie
Posts: 20831
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saibabaa Pragada wrote:Hi, What is this logic/concept/feature called here ? The first statement after if condition is "dead Code". Second statement maybe "Dead Code" or "Unreachable code" as given below..What is happening here ?


I am pretty sure that if you follow the links in this topic, you'll get an explanation.... but here it is anyway.

The specification actually has relaxed rules for the "if" statement, in regards to the check for unreachability. The relaxed rules states that the compiler should not assume that the path is taken or not taken, even though it can be determined at compile time. The reason for this is to allow for debugging / test flags.



Henry
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the reason I am looking for. Thanks Henry.
Henry Wong wrote:The specification actually has relaxed rules for the "if" statement, in regards to the check for unreachability. The relaxed rules states that the compiler should not assume that the path is taken or not taken, even though it can be determined at compile time. The reason for this is to allow for debugging / test flags.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic