use try-with-resources to close a Closeable

Viewed 6262

I have a Map<Key, Closeable> and if a key is removed from the map I want to close the Closeable. Normally I have something like:

Closeable c = map.remove(key);
c.close();

My Eclipse warns me "Resource 'c' should be managed by try-with-resource", so is it better just to write the following?

try (Closeable c = map.remove(key)) {}

In my special implementation I have a subclass of Closeable, where close() does not throw an IOException, so no exception handling is needed.

2 Answers
Related