0Pricing
Kotlin Academy · Lesson

runCatching and Result<T>

Use runCatching to wrap exceptions in Result and process them functionally.

Result<T> in a Nutshell

Result<T> is Kotlin's wrapper for an operation outcome: either a success with a value of type T or a failure with a Throwable.

runCatching Basics

runCatching { ... } runs the block and wraps the outcome in a Result.

fun main() {
    val good = runCatching { 10 / 2 }
    val bad  = runCatching { 10 / 0 }
    println("good isSuccess = ${good.isSuccess}")
    println("bad isFailure  = ${bad.isFailure}")
}

All lessons in this course

  1. try/catch/finally as an Expression
  2. Creating Custom Exception Classes
  3. runCatching and Result
  4. Re-throwing and Exception Chaining
← Back to Kotlin Academy