Does Kotlin have checked exceptions? Do you need to declare or catch the exceptions a function might throw?
answer
- All Kotlin exceptions are unchecked
- No 'throws' keyword in signatures
- @Throws only for Java interop
- Compiler never forces try/catch
- Design choice: avoid boilerplate catch
basics
~10 sNo. In Kotlin every exception is unchecked. You are never forced to catch an exception or list it in the function signature. There is no 'throws' clause like in Java.
solid answer
~40 sKotlin has no checked exceptions. Every Throwable is treated as unchecked, so the compiler never forces you to wrap a call in try/catch or declare what it throws. There is no `throws` keyword in a function signature. This is a deliberate language decision: Roman Elizarov and the Kotlin team argued checked exceptions in large codebases lead to boilerplate `catch` blocks that swallow or rethrow. You can still catch anything you like with `try/catch`. When calling Kotlin from Java, you add the `@Throws(IOException::class)` annotation so the generated Java method declares the checked exception, satisfying Java callers. Without it, Java code that needs the declaration won't compile against your Kotlin API. The annotation changes only the generated bytecode signature; it has no effect on Kotlin callers.
go deeper
Knows there are no checked exceptions and try/catch is optional.
Explains the design rationale and mentions @Throws for interop.
Articulates the bytecode-signature effect of @Throws and when Java callers break without it.
Discusses API-design tradeoffs of unchecked-only exceptions and team conventions for documenting throwable failures.
## Checked vs unchecked exceptions In Java, exceptions are split into two groups: - **Checked exceptions** (subclasses of `Exception` but not `RuntimeException`, e.g. `IOException`): the compiler *forces* the caller to either catch them or declare them with `throws` in the method signature. - **Unchecked exceptions** (subclasses of `RuntimeException` and `Error`): no compiler enforcement. ## Kotlin's rule: everything is unchecked Kotlin treats **all** exceptions as unchecked. The compiler never requires you to: - wrap a call in `try/catch`, or - declare thrown types in the function signature (there is no `throws` keyword in Kotlin at all). This means code like reading a file compiles with no ceremony: ```kotlin fun read(path: String): String = java.io.File(path).readText() // can throw IOException, no catch required ``` ## Why this design The Kotlin team's position is that checked exceptions, in large codebases, encourage low-quality `catch` blocks that either swallow the exception or blindly rethrow it, adding boilerplate without improving reliability. So Kotlin removed the enforcement while keeping the `try/catch` machinery for when you genuinely want to handle a failure. ## Interop: `@Throws` Because Java still cares about checked exceptions, Kotlin provides the `@Throws` annotation. It does not change Kotlin semantics; it only adds a `throws` clause to the **generated Java bytecode signature** so Java callers can (and must) handle it: ```kotlin @Throws(java.io.IOException::class) fun loadConfig(path: String): String = java.io.File(path).readText() ``` Without `@Throws`, Java code calling `loadConfig` and surrounding it with `catch (IOException e)` will fail to compile, because Java thinks the method never throws a checked exception. ## Key terms - **Throwable**: the root type of everything you can `throw` or `catch`. - **Unchecked**: not enforced by the compiler. - **`@Throws`**: interop-only annotation for Java callers.
- If exceptions are unchecked, how does a Java caller know to catch a Kotlin function's IOException?It doesn't, unless you annotate the Kotlin function with @Throws(IOException::class), which emits a Java 'throws' clause in the bytecode signature.
- Does @Throws change behavior for Kotlin callers?No. Kotlin callers are still never forced to catch it; @Throws affects only the generated Java signature.
Java's checked exceptions are like a form you must fill in at every door; Kotlin removes the form but the doors are still there if you choose to use them.
saying these in an interview costs you the question
- Saying Kotlin has checked exceptions like Java
- Claiming the compiler forces you to catch IOException
- Thinking @Throws makes Kotlin callers catch the exception
- Believing there is a 'throws' keyword in Kotlin signatures