KT.THROWING_EXCEPTIONS_WITHOUT_MESSAGE_OR_CAUSE

Exception without arguments or further description

This rule reports all exceptions which are thrown without arguments or further description. Exceptions should always call one of the constructor overloads to provide a message or a cause. Exceptions should be meaningful and contain as much detail about the error case as possible. This will help to track down an underlying issue in a better way.

Noncompliant Code

Copy
fun foo(bar: Int) {
    if (bar < 1) {
        throw IllegalArgumentException()
    }
    // ...
}

Compliant Code

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

Options

  • excludes (default: ['**/test/**', '**/androidtest/**', '**/commontest/**', '**/jvmtest/**', '**/jstest/**', '**/iostest/**'])

    path filter

  • exceptions (default: ['arrayindexoutofboundsexception', 'exception', 'illegalargumentexception', 'illegalmonitorstateexception', 'illegalstateexception', 'indexoutofboundsexception', 'nullpointerexception', 'runtimeexception', 'throwable'])

    exceptions which should not be thrown without message or cause

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