Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A try-catch construct in Kotlin is an expression used for exception handling to intercept and process objects that inherit from the Throwable class. Unlike Java, Kotlin does not distinguish between checked and unchecked exceptions; all exceptions in Kotlin are unchecked, meaning the compiler never enforces catching or declaring them via a throws clause.

Syntax

The construct consists of a mandatory try block, one or more optional catch blocks, and an optional finally block. At least one catch or finally block must be present.
try {
    // Block of code where exceptions may be thrown
} catch (e: IllegalArgumentException) {
    // Handler for a specific exception type
} catch (e: Exception) {
    // Handler for a broader exception type
} finally {
    // Block executed unconditionally after try/catch completion
}

Expression Evaluation

Because try-catch is an expression in Kotlin, it evaluates to a value that can be assigned to a variable or returned from a function.
  • Success Path: If no exception is thrown, the evaluated value is the last expression inside the try block.
  • Exception Path: If an exception is thrown and matched, the evaluated value is the last expression inside the corresponding catch block.
  • Finally Block: The contents of the finally block do not alter the result of the expression.
val result: Int = try {
    Integer.parseInt("100") // Evaluates to 100
} catch (e: NumberFormatException) {
    0 // Evaluates to 0 if an exception occurs
}

Catch Block Mechanics

  • Type Matching: The catch block parameter requires an explicit type declaration. Kotlin uses type checking (is operator logic implicitly) to match the thrown exception against the declared parameter type.
  • Evaluation Order: catch blocks are evaluated sequentially from top to bottom. The first block that matches the exception type (or a supertype of the exception) is executed.
  • Hierarchy Enforcement: To prevent unreachable code, handlers for derived exception classes (subclasses) must precede handlers for their base classes (supertypes).

The finally Block

The finally block executes after the try block and any executed catch blocks, regardless of whether an exception was thrown, caught, or left unhandled. While finally does not affect the value of the try-catch expression, an explicit return statement inside a finally block will override the return value of the enclosing function. This behavior alters the control flow and is generally considered an anti-pattern.

Type System Integration

At compile-time, the Kotlin type system computes the type of a try-catch expression by finding the common supertype of the try block and all catch blocks. If a catch block terminates by explicitly throwing an exception using a throw expression, that specific catch block evaluates to Kotlin’s Nothing type at compile-time. Because Nothing is a bottom type (a subtype of all other types in Kotlin), it does not alter the inferred type of the overall try-catch expression.
// The compiler infers 'value' as String.
// The try block evaluates to String, and the catch block evaluates to Nothing.
// The common supertype of String and Nothing is String.
val value: String = try {
    "Success"
} catch (e: Exception) {
    throw IllegalStateException(e) 
}
Master Kotlin with Deep Grasping Methodology!Learn More