I’m afraid there’s a big problem with the first example, which is that if an exception happens on or after the read, the finally
block executes. So far so good. But what if the fr.close()
then causes another exception to be thrown? This will “trump” the first exception (a bit like putting return
in a finally
block) and you will lose all information about what actually caused the problem to begin with.
Your finally block should use:
IOUtil.closeSilently(fr);
where this utility method just does:
public static void closeSilently(Closeable c) {
try { c.close(); } catch (Exception e) {}
}