java的System.exit(0)和System.exit(n)
2016-04-18 20:29
405 查看
终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止。该方法调用
Runtime类中的
exit方法。该方法永远不会正常返回。调用
System.exit(n)实际上等效于调用:
Runtime.getRuntime().exit(n)
在一个if-else判断中,如果我们程序是按照我们预想的执行,到最后我们需要停止程序,那么我们使用System.exit(0),而System.exit(1)一般放在catch块中,当捕获到异常,需要停止程序,我们使用System.exit(1)。这个status=1是用来表示这个程序是非正常退出。[/code]
源码:
/*** 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获得不同格式的系统时间
- 算法训练 最小乘积(基本型) java蓝桥杯
- Java异常分类
- spring配置文件详解--真的蛮详细
- Java基础(3)
- ThreadLocal真会内存泄露?
- java集合-EnumMap与EnumSet
- Java自动装拆箱boxing与unboxing
- Java内存回收算法
- 《Java JDK8学习笔记》读书笔记(4)
- 【小笨鸟看JDK1.7集合源码之二】ArrayList源码剖析
- #java读书笔记#集合框架3
- 多态
- Struts2中自定义类型转换器
- java IO流
- Spring MVC 中 HandlerInterceptorAdapter的使用
- Java中HashMap详解
- javaDAY5
- 个人对设计模式的理解-->建造者模式
- HashMapUtil java项目中缓存的使用