aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Unreachable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Unreachable" Watch "Unreachable" New topic
Author

Unreachable

Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
Hi,



Gives output:

a
Caught.
Done.
b
Caught.
Done.

So...

***Unreachable 1
If you uncomment the line marked "Unreachable 1", compilation fails, because the line is, indeed, unreachable.

***Unreachable 2
However, the line marked "unreachable 2", compiles like it's ringing a bell.

Why doesn't the compiler balk at the second example? Is it merely because the compiler is not clever enough to see that "unreachable 2", is indeeed unreachable?

Is there any easy rule of thumb to say when something will be considered unreachable and therefore cause the compiler to roll over onto its back, with its legs kicking wildy in the air?

Cheers,

Si.


SCJP 1.4 (93%)<br />SCJD (In progress. It can run, but it can't hide...)
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298

Why doesn't the compiler balk at the second example? Is it merely because the compiler is not clever enough to see that "unreachable 2", is indeeed unreachable?

U r right, compiler is indeed not clever enough to find an unreachable statement there. In ur first example compiler can easily make out that whatever be the scenario there has to be an exception thrown after which any statements following it in the block turns out to be unreachable.

But in the second example, compiler cannot decide if the method can throw an exception or not, because methods can only be run during run time. So whatever problem is visible during compile time, it raises the issue.
Hope u got it.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272

Is there any easy rule of thumb to say when something will be considered unreachable

I don't know about "easy", but the rule is in the Java Language Specifications section 14.20:
http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#237365

A Java compiler could be more clever than the JLS requires and find more unreachable statements, but that would introduce compatibility problems when working programs could not be compiled on that compiler.


Mike Gershman
SCJP 1.4, SCWCD in process
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Mike]: A Java compiler could be more clever than the JLS requires and find more unreachable statements...

There isn't a lot of leeway in the rules here - Java compilers are generally not allowed to find any more unreachable statements, other than the ones specifically declared unreachable in those rules. For example the following code is required to be compilable, even though a "more clever" compiler could determine that the line x=3 will never execute:
Likewise this code is required to compile, even though k = 2 will never execute:
There may be some areas of ambiguity, or some cases where compilers do not operate exactly according to the rules as stated. But the intent of the rules here is to be very specific about what may and may not compile.


"I'm not back." - Bill Harding, Twister
Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
Thanks for the feedback guys.

Cheers,

Si.
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi jim

Technically those code which u gave should not compile.But they are compiling and running without any error.

what will be the answer we need to give if they asked in the exam.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Parameswaran]: Technically those code which u gave should not compile

Technically those code examples should compile, because I took them from the JLS which specfically said that those particular code examples should compile. Which is why my post said the code is required to compile. Follow the link Mike gave.
[ April 08, 2005: Message edited by: Jim Yingst ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unreachable
 
Similar Threads
ghost - unreachable statement
examlab(question-53)-unreachable statement NAO?
Exception
About one Exception quesion,help
Exception