Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin Native equivalent of System.exit(-1)

In the following Kotlin/JVM program System.exit(-1) stops the execution of the program with an error exit code:

fun main(args: Array<String>) {
    if (args.size < 2) {
        println("too few args!")
        System.exit(-1)
    }
    println("Hello, ${args[1]} from ${args[0]}")
}

Kotlin/Native does not have access to any Java classes, including System. So what would the equivalent function be for a Kotlin/Native program to stop execution of the program with an error code?

like image 656
Nate Vaughan Avatar asked Dec 02 '17 03:12

Nate Vaughan


People also ask

How to stop execution of a Kotlin program with an error exit code?

In the following Kotlin/JVM program System.exit (-1) stops the execution of the program with an error exit code: fun main (args: Array<String>) { if (args.size < 2) { println ("too few args!")

How to terminate an app in Kotlin secondactivity?

And in SecondActivity the button, calls exitProcess (2) (Kotlin’s equivalent to Java’s System.exit ()) in order to terminate the app. Now let’s follow this use case: the user opens the app, clicks on the first button, the second activity is displayed and now she clicks on btn_action defined above. What will happen to the application?

What is the exit code of a method?

It takes an exit code, which it passes on to the calling script or program. Exiting with a code of zero means a normal exit: We can pass any integer as an argument to the method. A non-zero status code is considered as an abnormal exit.

What is system exit in JVM?

Calling the System.exit method terminates the currently running JVM and exits the program. This method does not return normally. This means that the subsequent code after the System.exit is effectively unreachable and yet, the compiler does not know about it.


1 Answers

Use exitProcess:

import kotlin.system.exitProcess
...
exitProcess(exitCode)

Declaration and documentation in the Kotlin source code.

like image 82
Zoe stands with Ukraine Avatar answered Oct 04 '22 14:10

Zoe stands with Ukraine