简体   繁体   中英

Why java.lang.AutoCloseable interface is added in Java 1.7

java.lang.AutoCloseable interface is same as java.io.Closeable interface. So what is need to add new java.lang.AutoCloseable interface in Java 1.7.

in Java SE 7 and later, implements the interface java.lang.AutoCloseable. Because the BufferedReader instance is declared in a try-with-resource statement, it will be closed regardless of whether the try statement completes normally or abruptly (as a result of the method BufferedReader.readLine throwing an IOException).

It's used for auto closing the resources regardless of whether the try statement completes normally or abruptly in Java 7, check here for more details.

And,

The Closeable interface extends the AutoCloseable interface. The close method of the Closeable interface throws exceptions of type IOException while the close method of the AutoCloseable interface throws exceptions of type Exception. Consequently, subclasses of the AutoCloseable interface can override this behavior of the close method to throw specialized exceptions, such as IOException, or no exception at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM