zoukankan      html  css  js  c++  java
  • System.exit()方法的作用

    查看java.lang.System的源码。我们能够看到System.exit()这种方法等价于Runtime.exit(),代码例如以下:

    1. /**
       * Terminates the currently running Java Virtual Machine. The
       * argument serves as a status code; by convention, a nonzero status
       * code indicates abnormal termination.
       * <p>
       * This method calls the <code>exit</code> method in class
       * <code>Runtime</code>. This method never returns normally.
       * <p>
       * The call <code>System.exit(n)</code> is effectively equivalent to
       * the call:
       * <blockquote><pre>
       * Runtime.getRuntime().exit(n)
       * </pre></blockquote>
       *
       * @param      status   exit status.
       * @throws  SecurityException
       *        if a security manager exists and its <code>checkExit</code>
       *        method doesn't allow exit with the specified status.
       * @see        java.lang.Runtime#exit(int)
       */
      public static void exit(int status) {
      	Runtime.getRuntime().exit(status);
      }
    从方法的凝视中能够看出此方法是结束当前正在执行的Java虚拟机,这个status表示退出的状态码,非零表示异常终止。注意:无论status为何值程序都会退出,和return 相比有不同的是:return是回到上一层,而System.exit(status)是回到最上层。


    System.exit(0):不是非经常见,做过swing开发的可能用过这方法,一般用于Swing窗口关闭button。(重写windowClosing方法时调用System.exit(0)来终止程序,Window类的dispose()方法仅仅是关闭窗口。并不会让程序退出)。
    System.exit(1):很少见,一般在Catch块中会使用(比如使用Apache的FTPClient类时,源代码中推荐使用System.exit(1)告知连接失败),当程序会被脚本调用、父进程调用发生异常时须要通过System.exit(1)来告知操作失败,默认程序终于返回的值返是0,即然发生异常默认还是返回0,因此在这样的情况下须要手工指定返回非零。

  • 相关阅读:
    试用solace 消息平台
    mqtt5 share subscription 简单说明
    文件批量重命名神器:Bulk Rename Utility
    Elasticsearch入门,这一篇就够了
    burp suite使用(一) --- 抓包,截包,改包
    BurpSuite安装和配置
    ORA-01779: 无法修改与非键值保存表对应的列”中涉及的概念和解决方法
    一个非常有用的函数——COALESCE
    ORA-01779: cannot modify a column which maps to a non-key-preserved table
    Oracle批量、大量Update方法总结
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/5055349.html
Copyright © 2011-2022 走看看