Does close ever throw an IOException?

后端 未结 5 1821
一个人的身影
一个人的身影 2020-11-29 09:19

After providing some answers here, and reading some comments, it would seem that, in practice IOException is never thrown on close for file I/O.

Are there any cases

5条回答
  •  有刺的猬
    2020-11-29 09:51

    It's specifically FileInputStream.close which does not throw, even if your hard drive is on fire. Presumably it is the same for socket input. For output streams you may also be flushing. Until relatively recently [see timestamps] BufferedOutputStream used to fail to close the underlying stream if flush threw.

    (@MaartenBodewes would like me to point out that FileInputStream.close not throwing is not specified by the API docs. At the time of the post it was customary to elide the clause mentioning that this related to the Sun JDK (now known as Oracle JDK and OpenJDK). It appears that an obscure former reimplementation called Apache Harmony which Android used to use may have had different behaviour. Potentially other implementations, or versions of OpenJDK, may also throw.)

提交回复
热议问题