Kotlin

Kotlin

Made by DeepSource

Exceptions should not be rethrown after being wrapped in a new instance of the same type KT-W1008

Anti-pattern
Major

Exceptions should not be wrapped inside the same exception type and then rethrown.

Instead, wrap the exception in a more meaningful exception type.

Bad Practice

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
    }
}

Recommended

Avoid rethrowing a new instance of the same exception after catching it.

fun foo() {
    try {
        // ...
    } catch (e: IllegalStateException) {
        throw MyException(e)
    }
}