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.
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]: 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.
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.
Joined: Jan 30, 2000
[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 ]