Java finally block and throws exception at method level

Viewed 4168

In readFileMethod1, an IOException is explicitly catched before throwing it at the method level to ensure that the finally block is executed. However, is it neccessary to catch the exception? If I remove the catch block, shown in readFileMethod2, does the finally block get executed as well?

private void readFileMethod1() throws IOException {
    try {
        // do some IO stuff
    } catch (IOException ex) {
        throw ex;
    } finally {
        // release resources
    }
}

private void readFileMethod2() throws IOException {
    try {
        // do some IO stuff
    } finally {
        // release resources
    }
}
5 Answers
Related