KT.TOO_GENERIC_EXCEPTION_CAUGHT

Catch block for exceptions that have a type that is too generic

This rule reports catch blocks for exceptions that have a type that is too generic. It should be preferred to catch specific exceptions to the case that is currently handled. If the scope of the caught exception is too broad it can lead to unintended exceptions being caught.

Noncompliant Code

Copy
fun foo() {
    try {
        // ... do some I/O
    } catch(e: Exception) { } // too generic exception caught here
}

Compliant Code

Copy
fun foo() {
    try {
        // ... do some I/O
    } catch(e: IOException) { }
}

Options

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

    path filter

  • exceptionNames (default: ['arrayindexoutofboundsexception', 'error', 'exception', 'illegalmonitorstateexception', 'indexoutofboundsexception', 'nullpointerexception', 'runtimeexception', 'throwable'])

    exceptions which are too generic and should not be caught

  • allowedExceptionNameRegex (default: _|(ignore|expected).*)

    ignores too generic exception types which match this regex

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