3 ms·
>So, what's the point of closing the stream twice? The first stream.close() does not get executed if an IOException occurs.
by desdiv 12y ago
>So, what's the point of closing the stream twice?
The first stream.close() does not get executed if an IOException occurs.
- V-2 12y agoOf course, but the second one DOES execute whether the exception occurs or not. So the first call is redundant. If no exception occurs (which is most of the time, probably), the code will call stream.close(); stream = null; and after that it will pass that null stream to Utils.safeClose, which can't do anything to it at this point, as the stream is closed by now and we've dropped the reference to it anyway (by setting it to null in meantime) :) The code reads as if its author was under false impression that finally only executes if catch executes. But that's not true, and it would make no logical sense (why would the language distinguish between "catch" and "finally" at all in such case?)
- jacquesm 12y agoThe whole point of a finally block is to have the cleanup code in it that should be called regardless of whether or not an exception occurred in the first place. So the first stream.close should not be there if the 'finally' block already contains one, and it will be executed twice if an exception does not occur.