Exit Outside Main

ID

kotlin.exit_outside_main

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Kotlin

Tags

design, exit

Description

Reports calls to exitProcess() or System.exit() that appear outside a main() function.

Terminating the JVM abruptly from library code, service methods, or business logic prevents callers from handling errors, makes the code untestable, and bypasses shutdown hooks unexpectedly.

Rationale

// OK — only in the entry point
fun main(args: Array<String>) {
    if (args.isEmpty()) exitProcess(1)
}

// Bad — library function decides to kill the process
fun shutdown() {
    exitProcess(0)  // FLAW
}

class App {
    fun terminate() {
        exitProcess(2)  // FLAW
    }
}

Remediation

Replace exitProcess() / System.exit() outside main() with:

  • A thrown exception that lets callers decide the response.

  • A returned error code or sealed class result.

  • A registered shutdown hook (Runtime.getRuntime().addShutdownHook(…​)) if cleanup is needed.