androidengineers.Book a session

Exception Handling and Error Management

Best Practices (checked vs unchecked mindset)

article15 minMedium

The compiler does not define your recovery strategy

Kotlin treats exceptions as unchecked, so a method call can compile without handling failures declared by a Java API. You still need to read the contract and decide whether to recover, translate, or propagate.

fun readConfig(file: java.io.File): String = try {
    file.readText()
} catch (failure: java.io.IOException) {
    throw IllegalStateException("Unable to read configuration", failure)
}

This example treats missing configuration as fatal to the operation and retains the original cause. An optional configuration file might instead have a documented default, but permission errors and malformed content should not automatically become “no config.”

Handle failures at the layer with enough context to act. A low-level reader should not display a UI dialog; a screen should not need to decode raw database exceptions.

Exercise

Define policies for missing optional configuration, unreadable required configuration, and invalid configuration syntax. Write tests proving they produce distinct outcomes.

Check: retry only when the operation and failure are suitable for retry, and account for whether repeating it can duplicate side effects.

Reference: Exceptions

YOUR LEARNING JOURNEY

0 of 110 available lessons completed

Progress saved in this browser. No account needed.
Best Practices (checked vs unchecked mindset) | Kotlin Core Programming | Android Engineers