1
public class Test2 {
    public static void main(String args[]) {

        System.out.println(method());
    }

    public static int method() {
        try {
            throw new Exception();
            return 1;
        } catch (Exception e) {
            return 2;
        } finally {
            return 3;
        }
    }
}

in this problem try block has return statement and throws exception also... its output is COMPILER ERROR....

we know that finally block overrides the return or exception statement in try/catch block... but this problem has both in try block... why the output is error ?

1
  • 2
    Would you mind sharing with us the actual compiler error? Although this is a fairly trivial case, in general you decrease your chances of getting good answers by posting incomplete/ambiguous questions. Commented Sep 21, 2010 at 8:30

5 Answers 5

14

Because your return statement is unreachable - the execution flow can never reach that line.

If the throw statement was in an if-clause, then the return would be potentially reachable and the error would be gone. But in this case it doesn't make sense to have return there.

Another important note - avoid returning from the finally clause. Eclipse compiler, for example, shows a warning about a return statement in the finally clause.

Sign up to request clarification or add additional context in comments.

1 Comment

very well put. +1 for your answer
6

The compiler exception come from, like my Eclipse dude says

Unreachable code    Test2.java  line 11 Java Problem

The return statement of your main code block will never be reached, as an exception is thrown before.

Alos notice the return statement of your finally block is, at least, a design flaw, like Eclipse once again says

 finally block does not complete normally   Test2.javajava  line 14 Java Problem

Indeed, as a finally block is only here to provide some clean closing, it is not expected to return something that would override the result normally returned by the method.

1 Comment

+1. I understand you wanted to say "design flaw" instead of "design law" :)
2

The throw new Exception() will be called no matter what, so anything within the try block that follows the throw is Unreachable Code. Hence the Error.

Comments

2
public class Test2 {
    public static void main(String args[]) {

        System.out.println(method());
    }

    public static int method() {
        try {
            throw new Exception();
            return 1; //<<< This is unreachable 
        } catch (Exception e) {
            return 2;
        } finally {
            return 3;
        }
    }
}

It should eventually return 3.

Comments

0

Because 'return' keyword within try block is unreachable, that's why you are getting compile-time error. Omit that 'return' keyword from try block, and then run your program, your will successfully compile.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.