Exceptions should not be wrapped inside the same exception type and then rethrown.
Instead, wrap the exception in a more meaningful exception type.
In the code below, e
is rethrown but is wrapped in a new instance of IllegalStateException
again.
fun foo() {
try {
// ...
} catch (e: IllegalStateException) {
throw IllegalStateException(e) // rethrows the same exception
}
}
Avoid rethrowing a new instance of the same exception after catching it.
fun foo() {
try {
// ...
} catch (e: IllegalStateException) {
throw MyException(e)
}
}