Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Unreacheble Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Unreacheble Code" Watch "Unreacheble Code" New topic
Author

Unreacheble Code

Ivan Ivanoff
Ranch Hand

Joined: Jan 04, 2002
Posts: 56
Hi all, please take a look on this fun one :
1 public class AAA
2 {
3 public static void main(String []a)
4 {
5 System.out.println("A");
6 try
7 {
8 return;
9 }
10 catch(Exception e)
11 {
12 System.out.println("B");
13 }
14 System.out.println("C"); //14
15 }
16 }

Compiles fine - runs fine (printing "A").
Why compiler is not giving me the compile time error due to the statement on line 14 ?
-From my understending line 14 will never be reached in this program (unreacheble code) hmm...
Any help would be great.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
The compiler knows only the "possible paths" through a try/catch block. It knows that, the cod within the try block might throw an exception that will cause execution to transfer to the catch block. As the exception might be handled, it is possible for execution to continue from the catch block and execute that final line.
Even though we know the code is unreachable because we know that the catch block will never be executed, the compiler doesn't look that closely.
I hope that helps,
Corey


SCJP Tipline, etc.
Ivan Ivanoff
Ranch Hand

Joined: Jan 04, 2002
Posts: 56
Thank you Corey for your help.
But I still have a dark corner in my
"walking compiler" head.
According to your path : "compiler doesn't look that closely"...
How would I know when compiler will look that closely and when not ? I am still belive that my 'try{}' block will newet throw any exceptions ...
but may be some Severe JVM Exceptions - is' it possible in this case?
Any help would be great.
thx
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
For all the juicy details about "reachable" statements, check the JLS, §14.20 Unreachable Statements.
If you have any more questions after digesting all of that, let me know.
Corey
Himanshu Jhamb
Ranch Hand

Joined: Aug 01, 2001
Posts: 134
Ivan.
The compiler has not started executing the try block as yet & has no way of knowing what happens during runtime. All it sees is a return statement in the try block & it figures there is another execution path (the catch block), through which the line in question is reachable.
Had there been a return statement in the catch block as well, the line in question would have been unreachable.


Himanshu Jhamb<br />SCJP2 v1.2 (April 2002)<br />SCJP2 v1.4 (May 2002)
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Himanshu Jhamb:
Had there been a return statement in the catch block as well, the line in question would have been unreachable.

Or, by the same token, had there been a finally block with a return statement in it, you'd get a compiler error about the unreachable line.
Corey
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unreacheble Code
 
Similar Threads
overloading-overriding question
exception
Help Required with this code on threads
Just Don't Understand
Exception