0Pricing
Kotlin Academy · Lesson

runCatching

Capture exceptions as values.

runCatching is a free Kotlin Academy lesson on CoddyKit — lesson 2 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.

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 getOrElse and fold
  • Keep captured regions small

Next: building sealed result hierarchies.

Frequently asked questions

Is the “runCatching” lesson free?

Yes — the full text of “runCatching” 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 “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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

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.

All lessons in this course

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