7

I have the following code

public static void nocatch()
{
    try
    {
        throw new Exception();
    }
    finally
    {

    }
}

Which gives the error

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Unhandled exception type CustomException

Which is as expected, but adding a return statement in the finally block makes the error go away

public static void nocatch()
{
    try
    {
        throw new Exception();
    }
    finally
    {
        return; //makes the error go away! 
    }
}

Can someone please explain me what is going on? and why the error disappears?

Note : I wrote this code purely for experimental purposes!

codeMan
  • 5,730
  • 3
  • 27
  • 51
  • 1
    Related post: [try finally blocks execution](http://stackoverflow.com/q/18131447/1679863). Although that doesn't talk about the exception, but the reasoning for this is same as that. – Rohit Jain Mar 04 '15 at 18:06
  • 1
    Side-note: don't try running code that doesn't compile. There's really no benefit in doing so. Talk about the compile-time error, rather than the exception you get from running code that hasn't compiled to start with. – Jon Skeet Mar 04 '15 at 18:07
  • @JonSkeet I just wanted to point out the error that it caused, which is why I ran the code. – codeMan Mar 04 '15 at 18:22
  • @codeMan: But you didn't need to run the code - you just needed to note the compiler error. What benefit was there in running the code? It just obscured the compiler error message by adding cruft around it. – Jon Skeet Mar 04 '15 at 18:31
  • @jonskeet you are right compiler error should have been good enough – codeMan Mar 04 '15 at 18:41

2 Answers2

7

The error disappears because your code is now valid. (Not nice, but valid.)

If a finally block just has a straight return; statement, then the overall try/catch/finally or try/finally statement can't throw any exceptions - so you don't need to declare that it can throw an exception.

In your original code, your try block could (well, it will) throw Exception (or CustomException in your real code, apparently) - and that's a checked exception, which means you have to either catch it or declare that the method might throw it.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I wonder if the exception-killing behavior of returns in Java's `finally` blocks was a deliberately-provided feature, or if Gosling et al. simply didn't think it was worth the effort to prevent? I would hope it was the former, though if that was the case it would have been good for Gosling et al. to have documented an "unenforced requirement" that a `finally` block may only *legitimately* return in cases where the code knows that no exception could be pending (e.g. because a flag was set as the very last operation in a `try` or `catch` block). – supercat Mar 05 '15 at 17:14
0

As per the javadoc...

The finally block always executes when the try block exits. 
This ensures that the finally block is executed even if an unexpected exception occurs.

So your code is doing it correctly now.