Java 爪哇。System.exit(int 状态)。退出状态的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19349627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:21:54  来源:igfitidea点击:

Java. System.exit(int status). Value for exit status

javawindowsexit-code

提问by user471011

I saw a big amount of questions about System.exit(int status)on stackoverflow.

我看到的有关问题,一个很大的量System.exit(int status)stackoverflow

For example:

例如:

Difference in System. exit(0) , System.exit(-1), System.exit(1 ) in Java

系统差异。Java 中的 exit(0) , System.exit(-1), System.exit(1)

difference between System.exit(0) and System.exit(-1)

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

But I do not see answer on my partial question:

但我没有看到我的部分问题的答案:

Do we have any direct connection between exit statusand System Error Codesfor Microsoft products(Microsoft site)?

我们是否exit statusSystem Error CodesMicrosoft 产品(Microsoft 站点)之间有任何直接联系?

Sure, we do not have any direct connections, but:can we make this connection?

当然,我们没有任何直接联系,但是:我们可以建立这种联系吗?

If we run our java program on Windows OS and our java program has System.exit(int status)statement, can we use System Error Codes listor no?

如果我们在 Windows 操作系统上运行我们的 java 程序并且我们的 java 程序有System.exit(int status)语句,我们可以使用System Error Codes list还是不使用?

Is it good idea or not?

这是个好主意吗?

What do you think about it? Any other ideas?

你怎么看待这件事?还有其他想法吗?

Example:

例子:

  • Java program was run via command line on Wisdows OS;
  • Our java program catched exception like FileNotFoundException;
  • After this we made decision to close our program via System.exit(int state);
  • In this case can exit statusbe equal 2?
  • Java程序在Wisdows OS上通过命令行运行;
  • 我们的 Java 程序捕获了类似的异常FileNotFoundException
  • 在此之后,我们决定通过以下方式关闭我们的程序System.exit(int state)
  • 在这种情况下可以exit status等于2?

because(Microsoft site):

因为(微软网站):

ERROR_FILE_NOT_FOUND
2 (0x2)
The system cannot find the file specified. 

采纳答案by TwoThe

The exit status is a value of your choice. If you call your program and then react based on the exit value returned after it has finished execution, you are free to handle them as you wish.

退出状态是您选择的值。如果你调用你的程序,然后根据它执行完成后返回的退出值做出反应,你可以随意处理它们。

There is only one general rule, which is that exit status 0 assumes that your program completed without error. Furthermore a negative exit status is usuallyassumed to be a termination because of an unexpected error, while a positive exit status is assumed to be a (more or less) graceful termination.

只有一个通用规则,即退出状态 0 假定您的程序已完成且没有错误。此外,负退出状态通常被假定为由于意外错误而终止,而正退出状态则被假定为(或多或少)正常终止。

But again: if there is no batch file calling your code, no one will ever react on the exit status, and therefore you can return whatever you want, it will have no effect.

但同样:如果没有批处理文件调用您的代码,则没有人会对退出状态做出反应,因此您可以返回任何您想要的东西,它不会有任何影响。

回答by nanofarad

The exit code is simply informational and can be based on your design. The Microsoft codes are important because MS software may try to recognize them, likewise on Linux there is a different setof predefined agreed-upon codes. It won't hurt to use one of the sets.

退出代码只是提供信息,可以基于您的设计。Microsoft 代码很重要,因为 MS 软件可能会尝试识别它们,同样在 Linux 上也有一组不同的预定义商定代码。使用其中一组不会有什么坏处。