How to properly handle an IOException from close()

前端 未结 9 1555
滥情空心
滥情空心 2021-02-05 08:14

The Java I/O classes java.io.Reader, java.io.Writer, java.io.InputStream, java.io.OutpuStream and their various subclasses al

9条回答
  •  隐瞒了意图╮
    2021-02-05 08:35

    It depends on what you are closing. For example, closing a StringWriter does nothing. The javadocs are clear on that. In this case, you can ignore the IOException because you know that it will never be generated. Technically, you don't even need to call close.

    /**
     * Closing a StringWriter has no effect. The methods in this
     * class can be called after the stream has been closed without generating
     * an IOException.
     */
    public void close() throws IOException {
    }
    

    For other streams, log and handle the exception as appropriate.

提交回复
热议问题