0Pricing
Kotlin Academy · Lesson

Functional Error Handling

Map and recover.

Errors as Data Flows

Functional error handling treats failures as values you can transform, chain, and recover from — just like you transform successful values. The built-in Result offers operators for exactly this.

map

map transforms a successful value and leaves a failure untouched, propagating it unchanged.

fun main() {
    val r = Result.success(5).map { it * 2 }
    println(r.getOrNull())
    val f = Result.failure<Int>(Exception("x")).map { it * 2 }
    println(f.getOrNull())
}

All lessons in this course

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