June 2018
Intermediate to advanced
316 pages
6h 34m
English
Use the disposable pattern along with the try-finally construction to avoid resource leaks.
The following example demonstrates how to use them:
fun readFirstLine() : String? { ...... var bufferedReader: BufferedReader? = null return try { ...... bufferedReader = BufferedReader(inputStreamReader) bufferedReader.readLine() } catch (e: Exception) { null } finally { ...... bufferedReader?.close() }}
The Kotlin standard library already has extension functions that use this approach under the hood:
fun readFirstLine(): String? ...Read now
Unlock full access