Kotlin Academy · Lesson

The Result Type

Model success and failure.

Lesson 1 of 413 steps

The Result Type is a free Kotlin Academy lesson on CoddyKit. This is lesson 1 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.

Success or Failure

Many operations can fail. Kotlin's Result type models an outcome that is either a success holding a value or a failure holding an exception — without throwing.

Creating a Success

Wrap a value in a successful result with Result.success(value).

fun main() {
    val r: Result<Int> = Result.success(42)
    println(r.isSuccess)
    println(r.getOrNull())
}

Creating a Failure

Represent an error with Result.failure(exception). No exception is thrown until you choose to unwrap it.

fun main() {
    val r: Result<Int> = Result.failure(IllegalStateException("bad"))
    println(r.isFailure)
    println(r.exceptionOrNull()?.message)
}

getOrNull and exceptionOrNull

Inspect a result safely: getOrNull() returns the value or null, and exceptionOrNull() returns the error or null.

fun main() {
    val ok = Result.success("hi")
    val bad = Result.failure<String>(RuntimeException("oops"))
    println(ok.getOrNull())
    println(bad.exceptionOrNull()?.message)
}

getOrDefault

Provide a fallback value for failures with getOrDefault. Cleaner than null checks when you have a sensible default.

fun main() {
    val bad: Result<Int> = Result.failure(Exception())
    println(bad.getOrDefault(0))
    val ok: Result<Int> = Result.success(7)
    println(ok.getOrDefault(0))
}

getOrElse

getOrElse computes the fallback from the exception, letting you react differently to different errors.

fun main() {
    val r: Result<Int> = Result.failure(IllegalArgumentException("nope"))
    val value = r.getOrElse { e -> if (e is IllegalArgumentException) -1 else -2 }
    println(value)
}

getOrThrow

When you do want the exception, getOrThrow() returns the value or rethrows the stored exception.

fun main() {
    val ok = Result.success(10)
    println(ok.getOrThrow())

    try {
        Result.failure<Int>(IllegalStateException("fail")).getOrThrow()
    } catch (e: Exception) {
        println("caught: " + e.message)
    }
}

Returning a Result

A function can return Result to make failure explicit in its signature, forcing callers to handle both outcomes.

fun parse(s: String): Result<Int> {
    val n = s.toIntOrNull()
    return if (n != null) Result.success(n)
           else Result.failure(NumberFormatException(s))
}

fun main() {
    println(parse("123").getOrNull())
    println(parse("x").exceptionOrNull()?.message)
}

Result vs Exceptions

Exceptions are invisible in signatures and disrupt control flow. Result makes failure a value you pass and transform like any other — explicit, composable, and hard to ignore.

When to Use Result

Use Result for recoverable, expected failures you want callers to handle. Keep throwing exceptions for truly exceptional, programmer-error situations like contract violations.

A Value, Not a Jump

The key shift: an error becomes a normal return value. Next you will learn runCatching, which captures thrown exceptions into a Result automatically.

Quick Check

Test your understanding of the Result type.

Recap

You learned the Result type:

  • Create with Result.success / Result.failure
  • Inspect with getOrNull, exceptionOrNull
  • Recover with getOrDefault, getOrElse, getOrThrow
  • Return it to make failure explicit

Next: runCatching.

Free to start

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 “The Result Type” lesson free?

Yes — any 3 lessons of the Kotlin Academy learning path, including “The Result Type”, 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 “The Result Type”?

Model success and failure. 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 1 of 4.

How long does the “The Result Type” 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