KT.EXIT_OUTSIDE_MAIN

Calls of System.exit() or exitProcess() outside the main function

Flags use of System.exit() and Kotlin's exitProcess() when used outside the main function. This makes code more difficult to test, causes unexpected behaviour on Android, and is a poor way to signal a failure in the program. In almost all cases it is more appropriate to throw an exception.

Noncompliant Code

Copy
fun randomFunction() {
    val result = doWork()
    if (result == FAILURE) {
        exitProcess(2)
    } else {
        exitProcess(0)
    }
}

Compliant Code

Copy
fun main() {
    val result = doWork()
    if (result == FAILURE) {
        exitProcess(2)
    } else {
        exitProcess(0)
    }
}

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