Functional Error Handling
Map and recover.
Functional Error Handling is a free Kotlin Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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())
}mapCatching
If the transform itself might throw, mapCatching captures that exception into the result rather than letting it escape.
fun main() {
val r = Result.success("10").mapCatching { it.toInt() }
println(r.getOrNull())
val bad = Result.success("oops").mapCatching { it.toInt() }
println(bad.isFailure)
}recover
recover turns a failure back into a success by computing a value from the exception. A success passes through unchanged.
fun main() {
val r = Result.failure<Int>(Exception("down")).recover { 0 }
println(r.getOrNull())
val ok = Result.success(7).recover { 0 }
println(ok.getOrNull())
}recoverCatching
When the recovery logic might also fail, recoverCatching wraps that attempt, keeping everything as a result.
fun main() {
val r = Result.failure<Int>(Exception()).recoverCatching {
"5".toInt()
}
println(r.getOrNull())
}onSuccess and onFailure
These run side effects without changing the result, returning it for further chaining. Great for logging.
fun main() {
runCatching { "3".toInt() }
.onSuccess { println("ok " + it) }
.onFailure { println("fail " + it.message) }
.also { println("done") }
}Chaining a Pipeline
Operators compose into a clean pipeline: capture, transform, recover, all without nested try/catch.
fun main() {
val result = runCatching { "21".toInt() }
.map { it * 2 }
.recover { -1 }
.getOrThrow()
println(result)
}fold to a Final Value
End a pipeline with fold to collapse both branches into one concrete value, such as a user-facing message.
fun main() {
val msg = runCatching { "x".toInt() }
.map { it + 1 }
.fold(
onSuccess = { "result: " + it },
onFailure = { "failed: " + it.message }
)
println(msg)
}Mapping Over a Custom Type
You can give your own sealed result type the same ergonomics by writing extension functions like map.
sealed class Res<out T> {
data class Ok<T>(val v: T) : Res<T>()
data class Err(val msg: String) : Res<Nothing>()
}
fun <T, R> Res<T>.map(f: (T) -> R): Res<R> = when (this) {
is Res.Ok -> Res.Ok(f(v))
is Res.Err -> this
}
fun main() {
val r = Res.Ok(4).map { it * 3 }
println(r)
}Why Functional Style
Functional error handling keeps the happy path readable while errors flow through declaratively. There is no scattered try/catch, and the type system tracks failure end to end.
Putting It All Together
You now have a full toolkit:
map/mapCatchingto transformrecover/recoverCatchingto healonSuccess/onFailurefor effectsfoldto finalize
Quick Check
Test your understanding of functional error handling.
Recap
You learned functional error handling:
- Transform with
map/mapCatching - Heal with
recover/recoverCatching - Observe with
onSuccess/onFailure - Finalize with
fold
This completes the Result and error modeling course.
Frequently asked questions
Is the “Functional Error Handling” lesson free?
Yes — the full text of “Functional Error Handling” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.
What will I learn in “Functional Error Handling”?
Map and recover. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Functional Error Handling” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Result Type
- runCatching
- Sealed Result Hierarchies
- Functional Error Handling