runCatching
Capture exceptions as values.
runCatching is a free Kotlin Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.
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())
}Catching a Thrown Error
When the block throws, runCatching stores the exception instead of propagating it.
fun main() {
val r = runCatching { 10 / 0 }
println(r.isFailure)
println(r.exceptionOrNull()?.message)
}Wrapping Risky Parsing
A common use is wrapping calls that may throw, like parsing, so the failure is a value you can handle.
fun main() {
val good = runCatching { "42".toInt() }
val bad = runCatching { "abc".toInt() }
println(good.getOrNull())
println(bad.getOrNull())
}runCatching as a Receiver
You can call runCatching on a receiver object; inside the block this refers to it. Useful for chaining operations on a value.
fun main() {
val text = " 99 "
val r = text.runCatching { trim().toInt() }
println(r.getOrNull())
}Recovering Inline
Combine with getOrElse to supply a fallback right at the call site.
fun main() {
val n = runCatching { "oops".toInt() }.getOrElse { -1 }
println(n)
}fold for Both Branches
fold handles success and failure in one call, returning a single value from either branch.
fun main() {
val message = runCatching { "50".toInt() }.fold(
onSuccess = { "got " + it },
onFailure = { "error: " + it.message }
)
println(message)
}Refactoring try/catch
Where you might write a try/catch returning a value, runCatching is more concise and keeps the result as data.
fun safeDivide(a: Int, b: Int): Result<Int> =
runCatching { a / b }
fun main() {
println(safeDivide(8, 2).getOrNull())
println(safeDivide(8, 0).exceptionOrNull()?.message)
}Be Careful With Broad Catching
runCatching catches all Throwables, including ones you may not want to swallow. Avoid wrapping huge blocks; keep the captured region small and intentional.
Chaining Transformations
Because runCatching yields a Result, you can immediately chain map or recover on it. We explore those next lesson.
fun main() {
val r = runCatching { "7".toInt() }.map { it * 10 }
println(r.getOrNull())
}Why It Helps
runCatching bridges exception-throwing code and the value-based Result world. It lets you adopt functional error handling even when calling APIs that throw.
Quick Check
Test your understanding of runCatching.
Recap
You learned runCatching:
- Wraps a block's outcome into a
Result - Turns thrown exceptions into failures
- Combine with
getOrElseandfold - Keep captured regions small
Next: building sealed result hierarchies.
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “runCatching” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “runCatching”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.
What will I learn in “runCatching”?
Capture exceptions as values. 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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “runCatching” 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.