Does Kotlin have multi-catch?

Hi,

does Kotlin have multi-catch like Java? Something like this:

catch(SomeException | OtherException exc) {
  …
}

Would be nice.

Christian

23 Likes

Not at the moment, but it is on the table

6 Likes

Any news on the topic?

The above is still true.

I stumbled across this thread wondering the same thing…later I realized that a similar control flow can be achieved by nesting a when statement inside a catch block… Nothing revolutionary. I’d just like to share the idea.

try {
    // code
} catch(ex:Exception) {
    when(ex) {
        is SomeException,
        is AnotherException -> {
            // handle
        }
        else -> throw ex
    }
}

Eric

15 Likes

This certainly works, but is slightly less efficient as the caught exception is explicit to the jvm (so a non-processed exception will not be caught and rethrown which would be the corollary of your solution)

1 Like

Has there been any progress on this?

Having to use @surplus.et 's suggestion is very cumbersome and bloated.

Andrey’s message is still true. Multi-catch is not on the roadmap for 1.2, but it’s very likely that it will be added at a later time.

2 Likes

when it is ready, please add inspections as well to collapse into multicatch

Sure, we’ll do that.

3 Likes

Beginner here. Why this code

try {
    ...
} catch (e: Exception) {
    ...
} catch (e: JsonMappingException) {
    ...
} catch (e: UnrecognizedPropertyException) {
    ...
}

compiles fine? (I don’t think it works, but it does compile)

It looks perfectly fine for me. There is no multicatch here.

1 Like

Try listing the catch of the most specialised exception first, otherwise I suspect that catch (e: Exception) will catch all.

I think use only catch (e: Exception) enough.

Well, who knew that reading the documentation would be of any value :wink:

when matches its argument against all branches sequentially until some branch condition is satisfied

Of course in my case it didn’t work, since Exception was listed first.

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

Thanks carlesic! That is a pretty reasonable work around! Wish I thought of it!

It’s a pity it requires kotlin.reflect

2 Likes

You can avoid reflect and adding new method by using whenclause, it is not much longer:

try {
    ...
} catch (e: Exception) {
    when(e){
         is SomeException, is OtherException -> {...}
         else -> {}
    }
}
6 Likes

To mimic multi-catch the else branch should rethrow the exception.

try {
    ...
} catch (e: Exception) {
    when(e) {
         is SomeException, is OtherException -> {...}
         else -> throw e
    }
}
5 Likes