KT.RETHROW_CAUGHT_EXCEPTION

Exception that are caught and then later re-thrown without modification

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.

Noncompliant Code

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

Compliant Code

Copy
fun foo() {
    try {
        // ...
    } catch (e: IOException) {
        throw MyException(e)
    }
    try {
        // ...
    } catch (e: IOException) {
        print(e)
        throw e
    }
    try {
        // ...
    } catch (e: IOException) {
        print(e.message)
        throw e
    }

    try {
        // ...
    } catch (e: IOException) {
        throw e
    } catch (e: Exception) {
        print(e.message)
    }
}

The content on this page is adapted from the Detekt Docs. Copyright ©2022 The Detekt Team. All rights reserved. https://detekt.dev/comments.html