4
public class ExceptionTest {
    public static void main(String[] args) {
        ExceptionTest et = new ExceptionTest();
        try {
            et.testMethod();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }   
    public int testMethod()  {
        try {           
            throw new Exception();
        }finally {
            return 4;
        }
    }

The above code is working fine, but when I change return type of testMethod() to void and changing the line return 4; to System.out.println("some print msg"); is causing compilation problem.

Can anybody please give solution for why it is giving compilation error?

user3114639
  • 1,895
  • 16
  • 42
surya prakash
  • 49
  • 1
  • 5

2 Answers2

4

The problem is that a return statement inside a finally block will cause any exception that might be thrown in the try block to be discarded.

When you remove the return from the finally then what the code is doing is to throw a checked exception which requires that you throw Exception or you catch it and this is the reason why there's a compiler error.

Look at

The behaviour of return in finally is described in Java Language Specification and is well explained here http://thegreyblog.blogspot.it/2011/02/do-not-return-in-finally-block-return.html

Community
  • 1
  • 1
Aris2World
  • 1,214
  • 12
  • 22
  • 1
    Can you elaborate more on *why* the exception is discarded? I didn't find any immediate explanation to why that happens in your links. – RaminS Jun 07 '16 at 13:36
  • In the question it says that on removing the return statement, the problem occurs. – cslotty Jun 07 '16 at 13:39
  • @cslotty When you `return`, the exception is apparently *discarded* and need not be caught. This is why you don't get the error that you would expect to get. – RaminS Jun 07 '16 at 13:40
  • Yeah but that was not the question - the question was "Can anybody please give solution for why it is giving compilation error?" -> The compilation error goes away on adding "throws Exception". – cslotty Jun 07 '16 at 13:48
  • 1
    @cslotty The word "solution" is misused by the OP and should be "answer". The rest of the question says *"...why it is giving compilation error?"*. – RaminS Jun 07 '16 at 13:50
  • But the compilation error comes when not anymore returning the int..... and both the answer and the solution is, add "throws Exception" ;-) Let's agree on "it's much more sophisticated to ask why there is such a difference between returning int and returning nothing", but unfortunately, the question wasn't clear. ;-) – cslotty Jun 07 '16 at 13:59
  • @cslotty The question specifies that he made two alternative: the 1st he return, the 2nd he doesn't return. The 1st swallow the checked exception so there's no problem for the compiler while the 2nd doesn't swallow and when a checked exception is thrown you have to catch it or to throw it. My second sentence explain this. – Aris2World Jun 07 '16 at 14:02
0

This code works in Java 8:

public class ExceptionTest {
    public ExceptionTest() {

    }

    public static void main(String[] args) {
        ExceptionTest et = new ExceptionTest();
        try {
            et.testMethod();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void testMethod() throws Exception {
        try {
            throw new Exception();
        } finally {
            //return 4;
            System.out.println("hello finally");
        }
    }
}

The only compilation problem for me was the missing "throws Exception" in the method declaration.

This is the output:

hello finally
java.lang.Exception
    at ExceptionTest.testMethod(ExceptionTest.java:17)
    at ExceptionTest.main(ExceptionTest.java:9)
cslotty
  • 1,696
  • 20
  • 28
  • You are missing the point. You don't get a compilation error with a method that returns an `int`, but when it is a `void` method you do, and the question is *why* this is the case. This is not an answer to the question. – RaminS Jun 07 '16 at 13:38
  • Well..... if you run the original code, you can see that the Exception is obviously ignored - it goes into the finally and returns the int. So why ask such a question if the behaviour is obvious and can be tested and proven? - Plus, the question was "Can anybody please give solution for why it is giving compilation error?" and that's what I did - the compilation error goes away on adding "throws Exception". – cslotty Jun 07 '16 at 13:47
  • It is not obvious at all *why* the exception is ignored, which is the question. – RaminS Jun 07 '16 at 13:48
  • Nope. "Can anybody please give solution for why it is giving compilation error?" -- The original question would have to be changed to reflect what you're saying. – cslotty Jun 07 '16 at 13:49