KT.THROWS_COUNT

Method exceed the number of 'throw' statements

Functions should have clear throw statements. Functions with many throw statements can be harder to read and lead to confusion. Instead, prefer limiting the number of throw statements in a function.

Noncompliant Code

Copy
fun foo(i: Int) {
    when (i) {
        1 -> throw IllegalArgumentException()
        2 -> throw IllegalArgumentException()
        3 -> throw IllegalArgumentException()
    }
}

Compliant Code

Copy
fun foo(i: Int) {
    when (i) {
        1,2,3 -> throw IllegalArgumentException()
    }
}

Options

  • max (default: 2)

    maximum amount of throw statements in a method

  • excludeGuardClauses (default: False)

    if set to true, guard clauses do not count towards the allowed throws count

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