try with resources

If a class implements AutoCloseable interface, you can use it within the "try" statement which guarantees the object will be closed.

The object will be closed at the end of the "try" block, before the "catch" or "finally" blocks.

try (SomethingAutoCloseable obj = new SomethingAutoCloseable()) {
}
// method "close" will be always called implicitly
try (...) {
} catch (Exception ex) {
  ...
}
try (...) {
} finally {
  ...
}
try (...) {
} catch (Exception ex) {
  ...
} finally {
  ...
}

viliam@bur.sk