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

查看:25
本文介绍了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 是任何非零退出代码的 standin.

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

推荐答案

当应用程序"实际上是一个子应用程序(例如 servlet、applet)时,不赞成使用 System.exit更大的 Java 应用程序(服务器):在这种情况下,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天全站免登陆