`
usenrong
  • 浏览: 505583 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

. System.exit(0)和System.exit(1) 和return 区别

    博客分类:
  • J2EE
阅读更多

1、System.exit(0)和System.exit(1)有什么区别?
2、在什么情况下执行System.exit()不会抛出异常?如果抛出了SecurityException异常,一般是因为什么原因?
3、如果我想中止当前运行的java程序,除了执行System.exit()还能用什么方法?

 

 

1。程序返回值不同。
2。抛出SecurityException是因为你调用了不允许的操作。比如在Applet中操作本地文件,或者在RMI程序中操作不允许的文件。Sun对于这个有专门的文章和解决方法。
3。对于java程序,运行System.exit()会终止JVM,所以Servlet和Applet中都不应该显示调用这个方法。

 

2個同為exit
public static void exit(int status)终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非零的状态码表示异常终止。零状态是终止整个程序。

 

 

 

如果在main方法中System.exit(0)与return 没有区别,都是终止程序。

如果是别的方法,那System.exit(0)直接终止程序,就算后面有代码也不执行了
而return则返回至调用该方法的地方,如果后面还有代码则继续执行

 

2.解析

查看java.lang.System的源代码,我们可以找到System.exit(status)这个方法的说明,代码如下:

 

/** * 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是非零参数,那么表示是非正常退出。

 

  1. System.exit(0)是将你的整个虚拟机里的内容都停掉了 ,而dispose()只是关闭这个窗口,但是并没有停止整个application exit() 。无论如何,内存都释放了!也就是说连JVM都关闭了,内存里根本不可能还有什么东西
  2. System.exit(0)是正常退出程序,而System.exit(1)或者说非0表示非正常退出程序
  3. System.exit(status)不管status为何值都会退出程序。和return 相比有以下不同点: return是回到上一层,而System.exit(status)是回到最上层

 

3.示例

在一个if-else判断中,如果我们程序是按照我们预想的执行,到最后我们需要停止程序,那么我们使用System.exit(0),而System.exit(1)一般放在catch块中,当捕获到异常,需要停止程序,我们使用System.exit(1)。这个status=1是用来表示这个程序是非正常退出。

分享到:
评论

相关推荐

    linux下system函数的简单分析

    简单分析了linux下system函数的相关内容,具体内容如下 int __libc_system &#40;const char *line&#41; { ...exit 0&#41; == 0; return do_system &#40;line&#41;; } weak_alias (__libc_system,

    linux 下 operating system chapter2 projet 的代码及编译方法

    In the module exit point, delete the elements from the linked list and return the free memory back to the kernel. Again, invoke the dmesg command to check that the list has been removed once the ...

    vb.net中的碎片发生器

    Option Strict OnImports System.MathPublic Class MandelbrotForm Inherits System.Windows.Forms.Form#... 1 Return Color.FromArgb(clr, 0, 0) Case 256 To 256 * 2 - 1 Return Color.FromArgb(255, clr - 256, 0...

    C# for CSDN 乱七八糟的看不懂

    } 有条件循环 private void button1_Click(object sender, System.EventArgs e) { int sum=0; int x=0; while ((sum) & (x)) { x++; sum+=x; } string s2=Convert.ToString(x); MessageBox.Show(s2); } 运行显示 14...

    C# 俄罗斯方块游戏

    this.panel1.Invalidate(new Rectangle(0,0,panel1.Width,100)); this.timer1.Enabled=false; this.paused=true; } private bool CreateBlock() { Point firstPos; Color color; if(this....

    EmvReader Java Code

    System.out.println(" example1&gt; paySmartTest 0 A0000005551010"); System.out.println(" example2&gt; paySmartTest 1 A000000555E010"); System.out.println(" example3&gt; paySmartTest 1 A0000000041010"); ...

    Unreliable Guide To Hacking The Linux Kernel

    1. Introduction................................................................................................................5 2. The Players ...........................................................

    VB自动关机源码.rar

    Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick If (Me.Visible = False) Then Me.Show() ...

    C#写gps中心服务处理程序

    /// &lt;/summary&gt;&lt;br&gt; private void menuItem1_Click(object sender,System.EventArgs e) { // } private void menuItem2_Click(object sender,System.EventArgs e) { ...

    java用类实现异常的处理的实例

    import java.util.*; class MyException extends Exception{ String message; MyException(int age){ message=age+"年龄异常";... System.exit(0); } System.out.println(a.getAge()); } }

    Java 2实用教程(第三版)实验指导与习题解答

    实验1 三角形、梯形和圆形的类封装 9 实验2 实例成员与类成员 12 实验3 使用package语句与import语句 13 上机实践4 继承与接口 15 实验1 继承 15 实验2 上转型对象 17 实验3 接口回调 18 上机实践5 字符串、时间与...

    带注释的Bootstrap.java

    System.exit(1); } } private ClassLoader createClassLoader(String name, ClassLoader parent) throws Exception { //CatalinaProperties解析$CATALINA_HOME\conf\catalina.properties, //并将...

    最新JAVA编程题全集_50题及答案

    System.out.println("第1个月的兔子对数: 1"); System.out.println("第2个月的兔子对数: 1"); int f1 = 1, f2 = 1, f, M=24; for(int i=3; i; i++) { f = f2; f2 = f1 + f2; f1 = f; System.out.println("第" +...

    ABAP Program Tips.pdf

    2.1.1 USEREXIT_KOMKBV1_FILL.................7 3.8.6 DOCUMENT FLOW...................................31 2.1.2 MASTER_IDOC_DISTRIBUTE..............7 3.8.7 MAINTAINING TRAILING SPACES WHEN DOWNLOADING TO...

    学生选修课程C语言系统设计学生选修课管理系统.doc

    return 0; } (二) 管理员功能模块 管理员的功能一共有四个功能:浏览,查询,添加,删除课程.运用字符串处理函数处理 密码的问题,用一个switch语句实现函数的调用. [程序] void Mangers() //管理员登录菜单 { int n,w=1...

    安卓单击两次返回键退出程序

    System.exit(0); } } Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); isExit = ...

    C#100例题 献给c#初学者

    给初学者的简单例题! private System.ComponentModel.IContainer components;... return 1; else if ((p.X &gt; PaddleRect.Left + PaddleRect.Width/4) && (p.X &lt;= PaddleRect.Left + PaddleRect.Width/2)) ...

    Java大学教程

    System.out.println("[7] Exit"); System.out.print("Enter choice [1-7]: "); choice = sc.next().charAt(0); System.out.println(); // process choice by calling helper methods switch(choice) { case ...

    Javaswing登录页面的实现

    System.exit(0); } else // 注册 if (str.equals("注 册")) { System.out.println("注 册"); } // 注册 else if (str.equals("忘记密码")) { System.out.println("忘记密码"); } else { System.out....

    基于Java实现的图像识别

    System.exit(1); }// end if } catch (InterruptedException ine) { ine.printStackTrace(); System.exit(1); } // end catch if(sourceImage == null) { sourceImage = bImage; ...

Global site tag (gtag.js) - Google Analytics