从 Java 7 开始,Closeable
接口已被改造为扩展AutoCloseable
接口,以便所有实现Closeable
接口的类都可以使用try-with-resources
语句。到目前为止,Closeable 接口可以自由地抛出任何异常,包括 InterrruptedException。try-with-resources
但是,在 Java 7 中,当退出 try 块后自动调用 close 方法时,语句中使用的 Closeable 接口的任何实例都可能抛出 InterruptedException,并且它InterruptedException
可能会被隐式调用抑制Throwable.addSuppressed(InterruptedException);
考虑到有人可能会在不知不觉中抑制其异常并且程序没有按应有的方式运行,这是否违反了 Java 的向后兼容性规则