python中exit,Java中的System.exit()

 2023-11-19 阅读 23 评论 0

摘要:The System.exit() method terminates the Java virtual machine. This method takes an argument, which is the status code passed to the caller. System.exit()方法終止Java虛擬機。 此方法采用一個參數,這是傳遞給調用方的狀態代碼。 A status cod

The System.exit() method terminates the Java virtual machine. This method takes an argument, which is the status code passed to the caller.

System.exit()方法終止Java虛擬機。 此方法采用一個參數,這是傳遞給調用方的狀態代碼。

A status code of zero signifies a normal exit whereas a status code of non-zero signifies an abnormal code.

狀態代碼為零表示正常退出,而狀態代碼為非零表示異常代碼。

python中exit,The code is not so important within Java itself. However, it’s common for scripts to rely on the exit codes of commands it invokes.

在Java本身中,代碼并不是那么重要。 但是,腳本通常依賴于它調用的命令的退出代碼。

Several non-zero codes can mean several different types of exits. This enables programmers to design their response system accordingly.

幾種非零代碼可能意味著幾種不同類型的出口。 這使程序員可以相應地設計其響應系統。

The System.exit() is like a harsh version of breaking out of loops using conditionals.

vb中exitfor、 System.exit()類似于使用條件語句打破循環的嚴酷版本。

System.exit(),狀態碼為0 (System.exit() with status code 0)

Let’s write a for loop and use System.exit(0) to exit out of the program with a status code of 0.

讓我們編寫一個for循環,并使用System.exit(0)以狀態代碼0退出程序。


public class Main {public static void main(String[] args) {for(int i=0;i<10;i++){System.out.println(i);if(i>5){System.exit(0);}}}
}
System Exit Code 0
Output
輸出量

0
1
2
3
4
5
6Process finished with exit code 0

We can see that the program terminates with an exit code of 0.

我們可以看到程序以退出代碼0終止。

以非零狀態碼退出 (Exiting with a non-zero status code )

關于java異常的說法?We can exit out of the program with any non zero exit code.

我們可以使用任何非零的退出代碼退出程序。


public class Main {public static void main(String[] args) {for(int i=0;i<10;i++){System.out.println(i);if(i>5){System.exit(10);}}}
}
System Code Non Zero

You can see that the exit code is printed as 10 in the output.

您可以看到出口代碼在輸出中顯示為10。

何時使用System.exit() (When to use System.exit())

It’s useful when you are calling a Java program from shell script. You can terminate the program in case of an abnormal scenario and pass on the status code to the caller shell script.

正確的是? 從外殼程序腳本調用Java程序時,此功能很有用。 您可以在異常情況下終止程序,并將狀態代碼傳遞給調用方外殼腳本。

結論 (Conclusion )

That is how Java.lang.System.exit() works. It is common to use it to exit loops and functions. Interestingly enough, System.exit() if not placed properly can render a certain portion of your code unreachable.

這就是Java.lang.System.exit()的工作方式。 通常使用它退出循環和功能。 有趣的是,如果未正確放置System.exit(),可能會使代碼的某些部分無法訪問。

翻譯自: https://www.journaldev.com/41960/system-exit-in-java

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/183293.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息