Add more flexible checking for rethrown exceptions.

Occasionally you need to intercept all exceptions, do something with them, and allow them to propogate. Doing this today is awkward. One way is to catch Throwable. But how do you rethrow it, without declaring Throwable in the current method and everything above it on the call stack? Java Puzzlers describes a couple of ways of rethrowing an exception such that the compiler's exception checking is circumvented. Some folks would resort to secret sun.Misc APIs. Some have advocated adding an optional Throwable parameter in a finally clause, which would take on the value of an exception if the try block is exited by an exception.

There is a much simpler approach. Consider this code:

    try {
        // some code
    } catch (final Throwable ex) {
        // some more code
        throw ex;
    }

Because this code throws an expression whose dynamic type is Throwable, the compiler currently thinks that the throw statement can throw any checked exception type. The code should be treated as if this try-finally block can only throw checked exceptions that are thrown in the try block. The trick is the final modifier on the catch parameter. Because the catch parameter is final, it can only hold exceptions that are thrown from the try block. Exception checking should take advantage of that fact.