Kotlin

Kotlin

Made by DeepSource

Proper logging should be preferred over printing stack trace of exceptions KT-W1002

Anti-pattern
Major

Avoid using printStackTrace to log exceptions.

This rule reports code that tries to print the stacktrace of an exception. Instead of simply printing a stacktrace a better logging solution should be used.

Bad Practice

fun foo() {
    Thread.dumpStack()
}

fun bar() {
    try {
        // ...
    } catch (e: IOException) {
        e.printStackTrace()
    }
}

Recommended

Log exceptions properly by using a popular logging library.

val LOGGER = Logger.getLogger()

fun bar() {
    try {
        // ...
    } catch (e: IOException) {
        LOGGER.info(e)
    }
}