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
- 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. - Q: runCatching?
A: Wraps a block inResult<T>—good for parsing or user input without try/catch noise.
Self-check
- Does Kotlin have checked exceptions?
- 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.