Does Kotlin have multi-catch?

While we wait for the multicatch support I’ve created this extension that may help someone:

fun <R> Throwable.multicatch(vararg classes: KClass<*>, block: () -> R): R {
     if (classes.any { this::class.isSubclassOf(it) }) {
         return block()
     } else throw this
}

Usage:

try {
    ...
} catch (e: Exception) {
    e.multicatch(SomeException::class, OtherException::class) {
        // Shared catch execution
    }
}
8 Likes