0
0
KotlinComparisonBeginner · 4 min read

Checked vs Unchecked Exception in Kotlin: Key Differences and Usage

In Kotlin, checked exceptions are not supported, meaning the compiler does not force you to handle or declare exceptions. All exceptions in Kotlin are unchecked exceptions, which are runtime exceptions that you can catch but are not required to declare or handle explicitly.
⚖️

Quick Comparison

This table summarizes the main differences between checked and unchecked exceptions, focusing on Kotlin's approach.

AspectChecked ExceptionUnchecked Exception (Kotlin)
Compiler EnforcementMust be declared or caughtNo enforcement; optional to catch
Exception TypeUsually extends Exception but not RuntimeExceptionExtends RuntimeException or Error
Handling RequirementMandatory handling or declarationOptional handling
Kotlin SupportNot supportedAll exceptions are unchecked
Use CaseRecoverable conditions expected to be handledProgramming errors or unexpected conditions
⚖️

Key Differences

Checked exceptions require the programmer to explicitly handle them by either catching the exception or declaring it in the function signature. This is common in Java but is not supported in Kotlin, which simplifies error handling by treating all exceptions as unchecked.

In Kotlin, exceptions are runtime exceptions, meaning the compiler does not force you to catch or declare them. This design choice reduces boilerplate code and makes the code cleaner but requires developers to be disciplined about handling exceptions properly.

Because Kotlin does not have checked exceptions, you can throw exceptions freely without declaring them, and you can catch exceptions optionally. This approach encourages handling exceptions where it makes sense, rather than everywhere.

⚖️

Code Comparison

Here is how a checked exception is handled in Java, which Kotlin does not support:

java
import java.io.IOException;

public class CheckedExceptionExample {
    // Method declares it throws IOException (checked exception)
    public static void readFile() throws IOException {
        throw new IOException("File not found");
    }

    public static void main(String[] args) {
        try {
            readFile();
        } catch (IOException e) {
            System.out.println("Caught checked exception: " + e.getMessage());
        }
    }
}
Output
Caught checked exception: File not found
↔️

Kotlin Equivalent

In Kotlin, the same task uses unchecked exceptions without declaring them:

kotlin
fun readFile() {
    throw Exception("File not found")
}

fun main() {
    try {
        readFile()
    } catch (e: Exception) {
        println("Caught unchecked exception: ${e.message}")
    }
}
Output
Caught unchecked exception: File not found
🎯

When to Use Which

Choose checked exceptions in languages like Java when you want the compiler to enforce handling of recoverable errors, ensuring they are not ignored.

In Kotlin, since only unchecked exceptions exist, use them to signal errors that can be caught optionally, keeping code cleaner and focusing on handling exceptions where it truly matters.

Use Kotlin's unchecked exceptions for most cases, but always document and handle exceptions thoughtfully to maintain robust code.

Key Takeaways

Kotlin does not support checked exceptions; all exceptions are unchecked.
Unchecked exceptions in Kotlin do not require declaration or mandatory catching.
Checked exceptions enforce handling at compile time, but Kotlin opts for simpler error handling.
Use unchecked exceptions in Kotlin to keep code clean and handle errors where necessary.
Be disciplined in catching exceptions in Kotlin to avoid unexpected crashes.