Skip to content

Files

Latest commit

 

History

History
39 lines (30 loc) · 863 Bytes

ExitOutsideMain.md

File metadata and controls

39 lines (30 loc) · 863 Bytes

Pattern: Exit outside main()

Issue: -

Description

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.

Example of incorrect code:

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

Example of correct code:

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

Further Reading