Further to being able to throw some other types of exceptions than IOException
one beautiful and common use case can be easily overseen:
One can override the interface to have no throws
declaration at all, thus permitting try
being written without explicit exception handling.
In our code we have an interface Searcher
declared in the following way
public interface Searcher<V> extends AutoCloseable {
Stream<V> search();
@Override
void close();
}
This permits the following use of Searcher
instances:
try (Searcher<Datatype> dataTypeSearcher = new DataTypeSearcher(query)) {
return dataTypeSearcher.search();
}
// without any catch statements
If no throws
declaration was present on AutoCloseable
, the above would be the only usage as it would not be possible to override the AutoCloseable
interface throwing an exception not declared on the parent. The way it is currently done, both options are possible.