Skip to main content
deleted 5 characters in body
Source Link

The example from aro is very good but if there are inheritances, it won't work like in Java.

Your answer inspired me to write an extension function for that. To also allow inherited classes you have to check for instance instead of comparing directly.

inline fun multiCatch(runThis: () -> Unit, catchBlock: (Throwable) -> Unit, vararg exceptions: KClass<out Throwable>) { try { runThis() } catch (exception: Exception) { val contains = exceptions.find { it.isInstance(exception) } if (contains != null) catchBlock(exception) else throw exception }} 

To see how to use, you also can have a look in my library on GitHub here

The example from aro is very good but if there are inheritances, it won't work like in Java.

Your answer inspired me to write an extension function for that. To also allow inherited classes you have to check for instance instead of comparing directly.

inline fun multiCatch(runThis: () -> Unit, catchBlock: (Throwable) -> Unit, vararg exceptions: KClass<out Throwable>) { try { runThis() } catch (exception: Exception) { val contains = exceptions.find { it.isInstance(exception) } if (contains != null) catchBlock(exception) else throw exception }} 

To see how to use, you also can have a look in my library on GitHub here

The example from aro is very good but if there are inheritances, it won't work like in Java.

Your answer inspired me to write an extension function for that. To also allow inherited classes you have to check for instance instead of comparing directly.

inline fun multiCatch(runThis: () -> Unit, catchBlock: (Throwable) -> Unit, vararg exceptions: KClass<out Throwable>) { try { runThis() } catch (exception: Exception) { val contains = exceptions.find { it.isInstance(exception) } if (contains != null) catchBlock(exception) else throw exception }} 

To see how to use, you can have a look in my library on GitHub here

Source Link

The example from aro is very good but if there are inheritances, it won't work like in Java.

Your answer inspired me to write an extension function for that. To also allow inherited classes you have to check for instance instead of comparing directly.

inline fun multiCatch(runThis: () -> Unit, catchBlock: (Throwable) -> Unit, vararg exceptions: KClass<out Throwable>) { try { runThis() } catch (exception: Exception) { val contains = exceptions.find { it.isInstance(exception) } if (contains != null) catchBlock(exception) else throw exception }} 

To see how to use, you also can have a look in my library on GitHub here