Kotlin

Kotlin

Made by DeepSource

Catch block should not require to check or cast the type of exception via is or as KT-W1000

Anti-pattern
Major

Avoid using is to check the type of exceptions, or as to cast exception types within catch blocks.

This issue is reported when a catch block uses a type check, or attempts to cast the caught exception to a specific type. Instead of catching generic exception types and then checking for specific exception types the code should use multiple catch blocks. These catch blocks should then catch the specific exceptions.

Bad Practice

fun foo() {
    try {
        // ... do some I/O
    } catch(e: IOException) {
        if (e is MyException || (e as MyException) != null) { }
    }
}

Recommended

Make sure there is only one @Inject constructor in every class.

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