KT.TOO_GENERIC_EXCEPTION_THROWN

Thrown exception that have a type that is too generic

This rule reports thrown exceptions that have a type that is too generic. It should be preferred to throw specific exceptions to the case that has currently occurred.

Noncompliant Code

Copy
fun foo(bar: Int) {
    if (bar < 1) {
        throw Exception() // too generic exception thrown here
    }
    // ...
}

Compliant Code

Copy
fun foo(bar: Int) {
    if (bar < 1) {
        throw IllegalArgumentException("bar must be greater than zero")
    }
    // ...
}

Options

  • exceptionNames (default: ['error', 'exception', 'runtimeexception', 'throwable'])

    exceptions which are too generic and should not be thrown

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