`

System.exit(0)与return的区别

阅读更多
问题:System.exit(0)与return的区别

1.try catch finally 若catch中有return ,finally中代码依然可以执行

参考文献:try catch 中添加 return ,fianlly中的代码是否依然会执行?

2.try catch finally 若catch中有System.exit(0),finally中代码不可以执行

	public static void main(String[] args) {

         try{
        	 int result = 0/0 ;
        	 System.out.println(result);
         }catch(Exception e){
        	 System.out.println("catch");
        	 System.exit(0);
         }finally{
        	 System.out.println("finally");
         }
         System.out.println("after finally");
	}


Console输出:
catch

3.区别
方法returnSystem.exit(n)
main()不能立即终止JVM运行立即终止JVM运行
被调用方法返回方法调用位置,其后的方法可以正常运行终止运行,其后方法不执行


多线程例子:
new myThread().start();
if(条件为真) return;
//此处的return并不能立即结束main。等待线程myThread
//若换成System.exit(数值);则立即无条件地结束。
//其它代码


4.总结
(1)在main()中的区别:
当用return;返回时,main()并不能立即运行结束,而用System.exit(数值);时,main()将立即无条件的结束
(2)system.exit(0)表示程序正常退出,system.exit(1)表示非正常退出,都没有返回值。return则返回至调用该方法的地方,如果后面还有代码则继续执行
(3)return返回上一层调用;System.exit(n)返回最上一层调用
参考文献:
http://bbs.csdn.net/topics/280057476
http://blog.csdn.net/mmllkkjj/article/details/6149043
分享到:
评论

相关推荐

    EmvReader Java Code

    System.out.println("#ERROR# Invalid reader index '"+args[0]+"'. Last valid index is " + (terminals.size()-1) ); System.exit(3); // throw new Exception( "Invalid reader index '"+iReader+"'. Last ...

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

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

    vb.net中的碎片发生器

    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox Friend WithEvents txtMin As System.Windows.Forms.TextBox Friend WithEvents txtMax As System.Windows.Forms.TextBox Friend WithEvents ...

    C# 俄罗斯方块游戏

    C#做的俄罗斯方块游戏using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Xml; using System.IO; using System.Security....

    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 ...

    linux下system函数的简单分析

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

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

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

    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() ...

    Javaswing登录页面的实现

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

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

    str.getChars(0,str.length(),c,0); if(c[0]>='a'&&c[0]) { c[0]=(char)(c[0]-32); } for(int i=1;i<=c.length-1;i++) { if(c[i]==' ') { c[i+1]=(char)(c[i+1]-32); } } str=new ...

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

    private System.Windows.Forms.Button cmdExit; private System.Windows.Forms.Button cmdSet;// = new byte[2048]; <br> private bool bTsFlag=false; public GprsServer() { <br> ...

    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...

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

    Java 2实用教程(第三版)实验指导与习题解答 清华大学出版社 (编著 耿祥义 张跃平) 实验模版代码 建议使用文档结构图 (选择Word菜单→视图→文档结构图) 上机实践1 初识Java 4 实验1 一个简单的应用程序 ...

    java飞信接口,FetionApi(无license限制,附可运行例子,简单文档说明,eclipse项目)

    //System.exit(1); }else{ mobile = args[0].trim(); pwd = args[1].trim(); } //System.out.println("mobile:" + mobile); //System.out.println("password:" + pwd); fetion = new Fetion...

    fetion-java-api(飞信的java api eclipse项目)

    //System.exit(1); }else{ mobile = args[0].trim(); pwd = args[1].trim(); } //System.out.println("mobile:" + mobile); //System.out.println("password:" + pwd); fetion = new Fetion...

    FFmpeg(liunx中amr转MP3工具)

    System.out.println("ffmpeg Process exitValue: " + exitVal); return true; } catch (Exception e) { System.out.println("ffmpeg exec cmd Exception " + e.toString()); } return false; }

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

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

    trycatchfinaly

    1. try的意思,就是试着...4. 如果在try或者catch之中,执行System.exit(0); 那么 finally不会被执行,此外的情况,不管是Error还是return,finally块都会执行到。 5. 建议不清楚的拷贝代码或者自己写一份执行了看看。

    基于Java实现的图像识别

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

    带注释的Bootstrap.java

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

Global site tag (gtag.js) - Google Analytics