Effective Kotlin: Item 9 — Prefer try-with-resources to try-finally

Java 8 brings try-with-resources to Java as an improvement to try-finally when dealing with Closeable resources. One of the reasons item 9 of Joshua Bloch’s book, Effective Java, suggests try-with-resources as a winner is when dealing with multiple resources the ensuing nest of try-finally blocks becomes ever harder to read.

As discussed in item 8, Kotlin doesn’t have try-with-resources. Instead, it has the use extension function. However, unlike try-with-resources this only acts on a single Closeable resource so…