Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unreacheble Code

 
Ivan Ivanoff
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ivan Ivanoff
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic