Why would I care about IOExceptions when a file is closed?

前端 未结 3 1668
迷失自我
迷失自我 2021-02-13 15:06

I\'ve see this sort of thing in Java code quite often...

try
{
    fileStream.close();
}
catch (IOException ioe)
{
    /* Ignore. We do not care. */
}

3条回答
  •  终归单人心
    2021-02-13 15:49

    I would at the very least log the exception.

    I've seen it happen occasionally, if the attempt to close the file fails due to it not being able to flush the data. If you just swallow the exception, then you've lost data without realizing it.

    Ideally, you should probably swallow the exception if you're already in the context of another exception (i.e. you're in a finally block, but due to another exception rather than having completed the try block) but throw it if your operation is otherwise successful. Unfortunately that's somewhat ugly to sort out :(

    But yes, you should at least log it.

提交回复
热议问题