相当于 System.exit(-1) 的 Kotlin Native

Posted

技术标签:

【中文标题】相当于 System.exit(-1) 的 Kotlin Native【英文标题】:Kotlin Native equivalent of System.exit(-1) 【发布时间】:2018-05-16 04:50:15 【问题描述】:

在以下 Kotlin/JVM 程序中,System.exit(-1) 停止程序的执行并出现错误退出代码:

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 无法访问任何 Java 类,包括 System。那么 Kotlin/Native 程序停止执行带有错误代码的程序的等效函数是什么?

【问题讨论】:

我猜你可以在 C 中正常使用 exit 函数。 【参考方案1】:

使用exitProcess:

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

Declaration and documentation 在 Kotlin 源代码中。

【讨论】:

以上是关于相当于 System.exit(-1) 的 Kotlin Native的主要内容,如果未能解决你的问题,请参考以下文章

System.exit和System.exit区别

请问大家在java中,System.exit(-1),System.exit(-2),到-4有啥区别呢?谢谢~

请问System.exit(-1)具体怎么用?为啥捕获异常的时候会用到?

System.exit(int status)

为啥即使在 System.exit(0) 之后也需要返回;

System.exit(-1)和return 的区别