System.exit的替代方案(1) [英] Alternatives to System.exit(1)

查看:849
本文介绍了System.exit的替代方案(1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于各种原因,在编写 Java应用程序时,调用 System.exit 是不受欢迎的,所以如何通知调用进程并非一切都是按照计划进行?

For various reasons calling System.exit is frowned upon when writing Java Applications, so how can I notify the calling process that not everything is going according to plan?

编辑:1是替换 -zero退出代码。

The 1 is a standin for any non-zero exit code.

推荐答案

使用 System.exit 是当'application'真的是一个更大的Java应用程序(服务器)的子应用程序(例如servlet,applet)时,我不赞成:在这种情况下, System.exit 可以停止JVM以及所有其他子应用程序。在这种情况下,抛出一个适当的异常,可以被应用程序框架/服务器捕获和处理是最好的选择。

The use of System.exit is frowned upon when the 'application' is really a sub-application (e.g. servlet, applet) of a larger Java application (server): in this case the System.exit could stop the JVM and hence also all other sub-applications. In this situation, throwing an appropriate exception, which could be caught and handled by the application framework/server is the best option.

如果java应用程序真正意味着是作为独立应用程序运行,使用 System.exit 没有任何问题。在这种情况下,设置退出值可能是向父进程传达失败或成功的最简单(也是最常用)的方式。

If the java application is really meant to be run as a standalone application, there is nothing wrong with using System.exit. in this case, setting an exit value is probably the easiest (and also most used) way of communicating failure or success to the parent process.

这篇关于System.exit的替代方案(1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆