A finally block performs cleanup after a protected try block, whether the operation completes normally or throws an exception. In Kotlin, it is especially useful for closing readers, unlocking locks, and releasing other resources. section 2.5.1 section 10.2.5
Closing a reader after parsing
fun readNumber(reader: BufferedReader): Int? { try { val line = reader.readLine() return Integer.parseInt(line) } catch (e: NumberFormatException) { return null } finally { reader.close() } } The try block reads and parses input, catch converts invalid input into null, and finally closes the reader in either case. Kotlin does not require a throws clause for this function, unlike Java's checked-exception style. section 2.5.1
Use finally when cleanup must not be skipped:
- Acquire a resource before try, then release it in finally. section 10.2.5
- Protect locks with the same pattern: lock before try, unlock in finally. section 10.2.5
- Handle coroutine cancellation the same way; a finally block closes resources even when cancellation throws CancellationException. section 15.2.8
Listing 2.28 Using try the same way you would in Java
import java.io.BufferedReader
import java.io.StringReader
fun readNumber(reader: BufferedReader): Int? { #1
try {
val line = reader.readLine()
return Integer.parseInt(line)
} catch (e: NumberFormatException) { #2
return null
} finally { #3
reader.close()
}
}
fun main() {
val reader = BufferedReader(StringReader("239"))
println(readNumber(reader))
// 239
}
Inspect the complete Kotlin example showing parsing, exception recovery, and reader closure. section 2.5.1
inline fun <T> Lock.withLock(action: () -> T): T { #1
lock()
try {
return action()
} finally {
unlock()
}
}
Compare explicit finally-based lock cleanup with Kotlin's higher-level withLock pattern. section 10.2.5
Listing 15.6 Using a finally block to close resources
val dbTask = launch {
val db = DatabaseConnection()
try {
delay(500.milliseconds)
db.write("I love coroutines!")
} finally {
db.close()
}
}
See finally protect a database connection when a coroutine is cancelled during its work. section 15.2.8

