• 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

examlab(question-53)-unreachable statement NAO?

 
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got the losing concept.
thanks Sheriff and Mr. Rancher for your reply that can be digested easily.
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic