AutoCloseable makes it possible to use the try-with-resources idiom:
```
import java.lang.Exception;
import java.io.IOException;
public class Main implements AutoCloseable {
void open() {
System.out.println("Open");
}
String read() throws IOException {
throw new IOException();
}
public void close() throws Exception {
System.out.println("Close");
}
public static void main(String[] args) {
try(Main m = new Main()) {
m.open();
m.read();
m.close();
} catch(Exception e) {
System.out.println("Exception");
}
}
}
```
Now you can say:
```
try(Main m = new Main()) {
m.open();
m.read();
m.close();
}
```
and JVM will call close() automatically for you.
Closeable is an older interface. To preserve backward compatibility, language designers decided to create a separate one. This allows not only all Closeable classes (like streams throwing IOException) to be used in try-with-resources, but also allows throwing more general checked exceptions from close().
When in doubt, use AutoCloseable, users of your class will be grateful.