Exception Handling

Kotlin is a modern, concise language that handles errors with try / catch — but with no checked exceptions and a try that's an expression, error handling stays clean and flexible.

Learn Exception Handling in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll catch exceptions, throw your own, validate with require , and use runCatching .

What You'll Learn in This Lesson

1️⃣ try, catch, and finally

Wrap risky code in try ; handle failures in catch ; run cleanup in finally . Because try is an expression, it can return a value — perfect for parsing with a fallback.

The finally block ran even though the first try threw — that's where you release resources or log, guaranteed.

2️⃣ Throwing, Custom Exceptions, and runCatching

Raise an error with throw , optionally of your own custom exception class carrying extra data. require guards inputs, and runCatching turns a risky call into a Result you can inspect.

runCatching let us treat the outcome as data — checking isFailure and supplying a default — without a single try / catch block.

Your turn. Replace the TODO , then run and compare.

Parse a string to an Int, returning a sentinel value when the input is invalid.

📋 Quick Reference — Exceptions

Practice quiz

Does Kotlin have checked exceptions like Java?

  • Yes, all exceptions are checked
  • No, Kotlin has no checked exceptions
  • Only for IOException
  • Only inside coroutines

Answer: No, Kotlin has no checked exceptions. Kotlin has no checked exceptions; you are never forced to catch or declare any.

What is special about try in Kotlin?

  • It must always have a finally block
  • It can only wrap one statement
  • It is an expression that can return a value
  • It cannot catch anything

Answer: It is an expression that can return a value. try is an expression; val x = try { ... } catch (e) { ... } is valid.

When does the finally block run?

  • Only when an exception is thrown
  • Only when no exception is thrown
  • Whether or not an exception was thrown
  • Never

Answer: Whether or not an exception was thrown. finally runs whether or not the try block threw, ideal for cleanup.

Which keyword raises an exception?

  • throw
  • raise
  • panic
  • fail

Answer: throw. throw raises an exception in Kotlin.

How do you typically define a custom exception?

  • Implement the Throwable interface only
  • Annotate a function with @Throws
  • Use a data class
  • Extend Exception (or a subclass)

Answer: Extend Exception (or a subclass). A custom exception extends Exception or a more specific subclass.

What does runCatching return?

  • A Result capturing success or failure
  • Always the value or null
  • Unit
  • A Boolean only

Answer: A Result capturing success or failure. runCatching returns a Result<T> capturing the value on success or the exception on failure.

What does require(condition) { message } do when the condition is false?

  • Returns null
  • Throws IllegalArgumentException with the message
  • Logs a warning and continues
  • Throws NullPointerException

Answer: Throws IllegalArgumentException with the message. require throws IllegalArgumentException when its condition is false.

For a try-as-expression, which value is produced on success?

  • null
  • The catch block's value
  • Always 0
  • The last expression of the try block

Answer: The last expression of the try block. On success the value is the last expression of the try block; on failure, the matching catch block.

Which is the recommended way to release a closeable resource?

  • Ignore it; the GC handles it instantly
  • Throw at the end
  • Put release in finally, or use use { }
  • Catch and rethrow

Answer: Put release in finally, or use use { }. Use finally for cleanup, or the use { } function for closeable resources.

What does getOrDefault(0) do on a failed Result?

  • Rethrows the exception
  • Returns the fallback value 0
  • Returns null
  • Returns true

Answer: Returns the fallback value 0. getOrDefault returns the supplied fallback when the Result is a failure.