androidengineers.Book a session

Exception Handling and Error Management

Exception Hierarchy in Kotlin

article15 minMedium

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.

Reference: Exceptions

YOUR LEARNING JOURNEY

0 of 110 available lessons completed

Progress saved in this browser. No account needed.
Exception Hierarchy in Kotlin | Kotlin Core Programming | Android Engineers