Skip to main content

Exceptions

In Kotlin, there is no difference between checked and unchecked exceptions. There are no checked exceptions in Kotlin so there is no need to specify this function throws at this exception. Kotlin's library still has a @Throws annotation. When we throw a checked exception from Java point of view in Kotlin and want to later handle it in Java, we need to add this annotation.

Java code calling foo() won't compile.

fun foo() {
throw IOException()
}

Java code calling bar() will compile.

@Throws(IOException::class)
fun bar() {
throw IOException()
}