0Pricing
Kotlin Academy · Lesson

runCatching

Capture exceptions as values.

Capturing Exceptions

runCatching runs a block and wraps its outcome in a Result: the return value becomes a success, any thrown exception becomes a failure. No try/catch needed.

Basic Usage

Pass a lambda; the last expression is the success value.

fun main() {
    val r = runCatching { 10 / 2 }
    println(r.isSuccess)
    println(r.getOrNull())
}

All lessons in this course

  1. The Result Type
  2. runCatching
  3. Sealed Result Hierarchies
  4. Functional Error Handling
← Back to Kotlin Academy