0
public class UnrechableCode {
        public static void main (String args[])
        {
            UnrechableCode uc=new UnrechableCode();
            try
            {
               System.out.println(1/0);
            }
            catch(Exception e)
            {
                System.out.print("Inside Catch");
                return ;
            }
            finally
            {
                 System.out.println("Inside Finally");
                 //return;
            }
            System.out.println("TEST");
        }
}

In Above Code When remove return Statement from Catch then the Statement after finally Block is not Executed,but it is reachable by parser. in the same scenario if i write return in finally block same statement is unreachable by parser,so if in the previous case statement is reachable by parser why statement is not executing.?

1 Answers1

2

The Java compiler has only a limited capability to predict whether a particular line of code in a program can ever be reached. This fact is not a fault of the designers of Java; it is a proven fact that it is not possible to predict the behavior of all possible programs of a general-purpose language so completely. The only choice the designers of Java could make is in just how limited this capability of the compiler should be.

See Why does Java have an "unreachable statement" compiler error? for a discussion of why the compiler has this ability at all, why it considers "unreachable code" an error, and why some things that could (relatively easily) have been detected as "unreachable code" are not considered to be "unreachable code" errors by the compiler.

In your particular case, the fact that 1/0 will always throw an exception, together with the fact that there is a return in the catch block that catches the exception, will prevent any execution of the program from ever writing "TEST" to the output. But the Java compiler does not try to account for exceptions thrown by such things as 1/0 when it looks for unreachable code. Therefore it acts as if there were a possible control path that executed the try block, did not throw an exception (so did not execute any code in the catch block), then executed the finally block and then the code after the finally block.

One of the things that the compiler does consider is that the finally block will always execute before the code that follows it, and that if there is a return in the finally block, the code after the return will not execute at all. Therefore a return in the finally block will make the code after it unreachable.

TL;DR: In one case, the "TEST" statement is unreachable for reasons that the Java compiler is designed to recognizes; in the other case, the "TEST" statement is unreachable for reasons that the Java compiler is not designed to recognize.

Community
  • 1
  • 1
David K
  • 3,147
  • 2
  • 13
  • 19
  • Compiler Showing its limitation in way that is different for two cases.when i write return statement in catch block the TEST statement is reachable (No Compile Time Error.) but it is not executed,and when i return in finally the TEST statement is not even reachable(Compile time Error),So why compiler showing two different scenario for the same return statement,and how it predict respective scenario for given cases. – Ravishankar Choudhary Jul 18 '16 at 05:37
  • What do you mean by "the same return statement"? You have two different places in the code where you put return statements. Putting a `return` in one place is _not_ the same as putting a `return` in another place, and you should expect that they _might_ have different consequences. I have added a step-by-step trace of how the unreachable-code analysis likely works for each of the two possible `return` statements. – David K Jul 18 '16 at 12:45
  • Here " Same Return Statement " means behavior of returning something to the method should always like in a same way,but in our case it is not,because of catch block return ,TEST statement is reachable (but not execute, why?) and because of finally block return ,TEST statement is not reachable,WHY?here return statement showing different behavior and this is what exact (same return statement)i am asking about.please distinguished more about why TEST value is not printing but reachable when i write return in catch but in case of finally for same return TEST statement is unreachable. – Ravishankar Choudhary Jul 19 '16 at 06:30
  • A `return` statement does more than just return something to the caller. It also _ends_ the execution of the method in which the `return` occurs. It is clear that `return; System.out.println("hello");` will behave differently than `System.out.println("hello"); return;`, is it not? In your example, the control flow is more complicated, but it is still important. Finally, remember that _the compiler never said that any of the code is reachable._ Only _sometimes,_ when there is unreachable code, the compiler will say it is unreachable. – David K Jul 19 '16 at 15:54
  • Well Mr David K, Thanks for your kind response, but still one thing that i want to know if compiler is silently accepted the code during compiling that means code have no errors,so please tell me one thing is it possible after compiling the code with no errors some line of code is not be a part of whole compiling and running process,if it is then tell me that cases as well. – Ravishankar Choudhary Jul 20 '16 at 07:06