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
}
}
The
finallystill gets executed, regardless of whether you catch the IOException. If all your catch block does is rethrow, then it is not necessary here.