| Author |
examlab(question-53)-unreachable statement NAO?
|
Arjun Srivastava
Ranch Hand
Joined: Jun 23, 2010
Posts: 431
|
|
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.
|
SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
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.
|
[My Blog]
All roads lead to JavaRanch
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
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.
|
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
|
 |
Arjun Srivastava
Ranch Hand
Joined: Jun 23, 2010
Posts: 431
|
|
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
Joined: Nov 24, 2005
Posts: 14672
|
|
Try this :
and this :
and this :
Do you see the difference ?
|
 |
Arjun Srivastava
Ranch Hand
Joined: Jun 23, 2010
Posts: 431
|
|
got the losing concept.
thanks Sheriff and Mr. Rancher for your reply that can be digested easily.
|
 |
Saibabaa Pragada
Ranch Hand
Joined: Oct 24, 2010
Posts: 162
|
|
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
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Saibabaa Pragada
Ranch Hand
Joined: Oct 24, 2010
Posts: 162
|
|
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.
|
 |
 |
|
|
subject: examlab(question-53)-unreachable statement NAO?
|
|
|