Kotlin

Kotlin

Made by DeepSource

Caught exceptions must not be rethrown KT-W1003

Anti-pattern
Major

Avoid throwing a caught exception without modifying it.

This rule reports all exceptions that are caught and then later re-thrown without modification. It ignores cases: 1. When caught exceptions that are rethrown if there is work done before that. 2. When there are more than one catch in try block and at least one of them has some work.

Bad Practice

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

Recommended

Ideally, don't rethrow caught exceptions. But if you have to, try to do something with the exception before rethrowing them.

fun foo() {
    try {
        // ...
    } catch (e: IOException) {
        // Wrapping the exception to a more specific one.
        throw MyException(e)
    }
}