Why did they add the AutoCloseable
and change the Closeable
as follows:
public interface Closeable extends AutoCloseable {
public void close() throws IOException;
}
Instead of just changing Closeable
as follows (without adding AutoCloseable
):
public interface Closeable {
public void close() throws Exception;
}
The advantages of the second solution would have been:
1) No limit of exceptions generated (see IOException
)
2) Closeable
itself could be used in try-with-resources without having to extend AutoCloseable
3) This wouldn't break existing code, because the implementation can only have exceptions that are more limited than the one defined in the interface
4) No overengineering
Is there any reason why instead they decided to go with the first solution and not the second one?
You simply cannot change the checked exception list of the already published methods. Otherwise the older code might become broken. For example such method might exist prior to Java 7:
After the change you propose this code would not compile. The backwards compatibility issues are taken very seriously by Java designers.