Skip to content
Learn Netverks

Lesson

Step 24/36 67% through track

exceptions-kotlin

Exceptions

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Exceptions: the syntax, patterns, and safety habits you need before advancing in Kotlin.

Teams still ship Exceptions in Kotlin codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Exceptions in contexts like: Android apps, Spring services, and shared KMP modules.

Write Kotlin in main.kt with fun main(), click Run on server—the dev runner kotlinc compiles to a JVM jar and java runs it; use println for output (requires JDK + kotlinc; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Kotlin uses JVM exceptions like Java—no checked exceptions. Use try/catch/finally, and idiomatically prefer runCatching or nullable results for expected failures.

try and throw

try {
    risky()
} catch (e: IllegalArgumentException) {
    println(e.message)
} finally {
    cleanup()
}

Throw with throw IllegalStateException("reason"). Nothing type (Nothing) marks functions that always throw.

Compared to Go

Go returns error values; Kotlin/JVM uses exceptions for unexpected failures—use errors as values for validation, exceptions for truly exceptional paths.

Important interview questions and answers

  1. Q: Checked exceptions in Kotlin?
    A: No—Kotlin does not require catch clauses for checked exceptions from Java; treat Java checked exceptions as unchecked at compile time.
  2. Q: runCatching?
    A: Wraps a block in Result<T>—good for parsing or user input without try/catch noise.

Self-check

  1. Does Kotlin have checked exceptions?
  2. What type does runCatching return?

Pitfall: Kotlin has no checked exceptions—still document thrown runtime exceptions for public APIs.

Interview prep

Checked exceptions?

Kotlin has none—all exceptions unchecked like runtime in Java.

try expression?

try/catch can return values.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • No checked exceptions?
  • try as expression?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump