Catch recoverable failures at the right layer
Kotlin uses Throwable as the root of throwable values. On JVM, Exception generally represents application failures, while Error covers serious runtime conditions that ordinary recovery code should not broadly swallow. Kotlin does not require callers to catch checked Java exceptions.
fun parseRequiredCount(text: String): Int = text.toInt()
fun main() {
try {
println(parseRequiredCount("oops"))
} catch (failure: NumberFormatException) {
println("Count must be a whole number")
}
}
This catches the failure the caller knows how to explain. Catching Throwable and returning zero would also hide unrelated defects and serious runtime failures. For routine parsing, toIntOrNull() is usually clearer than exception-driven control flow.
Exercise
Classify invalid user text, a missing file, a broken internal invariant, and an out-of-memory condition. Decide which should become a domain result, which should propagate, and where a user-facing message belongs.
Check: an exception hierarchy is not a retry policy; transient and permanent failures require application context.