-3

Can any one help me, what will happen when i will execute this program.

It should throw compile time error, but its not giving it.

public class Test {
public static void main(String args[]){
    int c = getCount();
    System.out.println(c);
}

private static int getCount() {
    try{
        throw new IOException();
    }finally{
        return 10;
    }
}

Thanks in advance !!

User_86
  • 265
  • 1
  • 4
  • 13
  • 2
    'It should throw compile time exception'. Why? –  Jul 02 '14 at 15:01
  • 2
    My qustion was 'Why?', your answer is 'Yes'. I am confused. –  Jul 02 '14 at 15:03
  • What is a "compile time exception". Do you mean a "compile time error"? – Raedwald Jul 02 '14 at 15:05
  • Why should it throw a 'compile time exception' (what is that?) when you try to throw an `IOException`? –  Jul 02 '14 at 15:09
  • possible duplicate of [Returning from a finally block in Java](http://stackoverflow.com/questions/48088/returning-from-a-finally-block-in-java) – ggovan Jul 02 '14 at 15:12

1 Answers1

10

It's because you have a return statement in your finally block - so the IOException is not actually going to be thrown out of your getCount() method. If a finally block completes abruptly (i.e. it throws an exception or has a return statement), that is the way that the whole try/finally or try/catch/finally block completes.

From JLS section 14.20.2:

If execution of the try block completes abruptly because of a throw of a value V, then there is a choice:

  • ...
  • If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and the throw of value V is discarded and forgotten).

And from section 11.2.2 (exception analysis of statements) - emphasis is mine:

A try statement (§14.20) can throw an exception class E iff either:

  • The try block can throw E, or an expression used to initialize a resource (in a try-with-resources statement) can throw E, or the automatic invocation of the close() method of a resource (in a try-with-resources statement) can throw E, and E is not assignment compatible with any catchable exception class of any catch clause of the try statement, and either no finally block is present or the finally block can complete normally
  • ...

In your case, the finally block cannot complete normally (i.e. get to the closing brace) due to the return statement, so the result of the analysis of the try statement is that it cannot throw the exception.

If you move the return 10; to after the finally block, you'll get the error you expected.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194