Netbeans中ExitSecurityException异常

来源:互联网 发布:淘宝客采集软件是什么 编辑:程序博客网 时间:2024/04/28 04:01
   自己做一个RCP的demo,很简单的功能,但就是死活调不通,跟踪定位在一句很平常的代码处:
   setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
   抛出ExitSecurityException类型的异常,消息是Illegal attempt to exit early.Baidu上没有找到任何有用信息。。。。Google之后,看了几篇E文帖子,恍然大悟,原因如下:
   EXIT_ON_CLOSE的窗口关闭方式会调用System.exit()方法,但是在Netbeans中,做了如下规定:
       IDE should refuse System.exit unless called from NbTopManager.exit
     这么做也是为了保证代码的健壮性,关闭了可能导致不良后果的System.exit()方法,在100% Pure Java Cookbook一书中,将misuse of System.exit归为了缺陷之一:
Pitfall: Misuse of System.exit
Explanation: The System.exit method forces termination of all threads in the Java virtual machine. This is drastic. It might, for example, destroy all windows created by the interpreter without giving the user a chance to record or even read their contents.
Solution: Programs should usually terminate by stopping all non-daemon threads; in the simplest case of a command-line program, this is as easy as returning from the main method. System.exit should be reserved for a catastrophic error exit, or for cases when a program is intended for use as a utility in a command script that may depend on the program’s exit code.
在Netbeans中,建议以下的关闭方式代替System.exit:
org.openide.LifecycleManager.getDefault().exit()
   令我不解的是在Netbeans中建立一般的应用程序窗体,就可以设置窗口关闭方式为EXIT_ON_CLOSE,不知道为什么Netbeans要这么区别对待~~
原创粉丝点击