100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Kotlin

Exception Handling in Kotlin

Learn how Kotlin handles runtime errors using try-catch-finally, unchecked exceptions, and try as an expression.

Coroutines & Error HandlingBeginner7 min readJul 8, 2026
Analogies

Introduction

Exception handling in Kotlin lets you deal with runtime errors gracefully instead of letting the program crash. Kotlin uses the familiar try-catch-finally structure inherited from Java, but with an important twist: all exceptions in Kotlin are unchecked. This means the compiler never forces you to catch or declare an exception with a throws clause, giving developers more flexibility while placing the responsibility of anticipating failures squarely on them.

🏏

Cricket analogy: A batsman doesn't need to declare in advance that a yorker might dismiss him; like Kotlin's unchecked exceptions, no umpire forces him to formally declare risk before facing Bumrah's delivery, though a poor shot can still crash the innings.

Syntax

kotlin
try {
    // code that might throw an exception
} catch (e: SomeException) {
    // handle the exception
} finally {
    // always executed, cleanup code
}

Explanation

The try block wraps code that could fail. If an exception is thrown, control jumps to the matching catch block, where the exception object 'e' can be inspected or logged. The finally block always runs, whether or not an exception occurred, making it ideal for closing resources. Unlike Java, Kotlin's try is an expression, meaning it can produce a value that gets assigned to a variable, with the last expression in the try or catch block becoming the result. Custom exceptions can be defined by extending Exception or RuntimeException.

🏏

Cricket analogy: The over itself is the try block facing risky deliveries; if Bumrah's yorker beats the bat, the umpire's raised finger is the catch inspecting what went wrong, the drinks break (finally) happens regardless of the dismissal, and a custom RunOutException could even be defined for that dismissal type.

Example

kotlin
class InvalidAgeException(message: String) : Exception(message)

fun validateAge(age: Int): Int {
    if (age < 0) throw InvalidAgeException("Age cannot be negative")
    return age
}

fun main() {
    val result = try {
        validateAge(-5)
    } catch (e: InvalidAgeException) {
        println("Caught: ${e.message}")
        -1
    } finally {
        println("Validation attempt finished")
    }
    println("Result: $result")
}

// Output:
// Caught: Age cannot be negative
// Validation attempt finished
// Result: -1

Key Takeaways

  • Kotlin uses try-catch-finally, similar in shape to Java's exception handling.
  • All exceptions in Kotlin are unchecked; there is no throws keyword or forced catching.
  • try is an expression in Kotlin and can return a value.
  • finally always executes, making it ideal for resource cleanup.
  • Custom exceptions extend Exception or RuntimeException.

Practice what you learned

Was this page helpful?

Topics covered

#Kotlin#KotlinProgrammingStudyNotes#Programming#ExceptionHandlingInKotlin#Exception#Handling#Syntax#Explanation#ErrorHandling#StudyNotes#SkillVeris