Pattern: Throwing new instance of same exception
Issue: -
Exceptions should not be wrapped inside the same exception type and then rethrown. Prefer wrapping exceptions in more meaningful exception types.
Example of incorrect code:
fun foo() {
try {
// ...
} catch (e: IllegalStateException) {
throw IllegalStateException(e) // rethrows the same exception
}
}
Example of correct code:
fun foo() {
try {
// ...
} catch (e: IllegalStateException) {
throw MyException(e)
}
}