`
txf2004
  • 浏览: 6864692 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Java异常捕获之try-catch-finally-return的执行顺序

 
阅读更多

情况1:try块中没有抛出异常try和finally块中都有return语句

 public static int NoException(){
  int i=10;
  try{
   System.out.println("i in try block is"+i);
   return --i;
  }catch(Exception e){
   --i;
   System.out.println("i in catch - form try block is"+i);
   return --i;
  }finally{
   
   System.out.println("i in finally - from try or catch block is"+i);
   return --i;
  }
 }


执行结果:

i in try block is10
i in finally - from try or catch block is9
the method value is8

执行顺序:执行try块,执行到return语句时,先执行return的语句,--i,但是不返回到main 方法,执行finally块,遇到finally块中的return语句,执行--i,并将值返回到main方法,这里就不会再回去返回try块中计算得到的值

情况2:try块中没有抛出异常,仅try中有return语句

代码:

	public static int NoException(){
		int i=10;
		try{
			System.out.println("i in try block is--"+i);
			return --i;
		}catch(Exception e){
			--i;
			System.out.println("i in catch - form try block is--"+i);
			return --i;
		}finally{
			
			System.out.println("i in finally - from try or catch block is--"+i);
			--i;
			System.out.println("i in finally block is--"+i);
			//return --i;
		}
	}


执行结果:

i in try block is--10
i in finally - from try or catch block is--9
i in finally block is--8
the method value is--9

顺序:try中执行完return的语句后,不返回,执行finally块,finally块执行结束后,返回到try块中,返回i在try块中最后的值

情况3:try块中抛出异常try,catch,finally中都有return语句

代码:

	public static int WithException(){
		int i=10;
		try{
			System.out.println("i in try block is--"+i);
			i = i/0;
			return --i;
		}catch(Exception e){
			System.out.println("i in catch - form try block is--"+i);
			--i;
			System.out.println("i in catch block is--"+i);
			return --i;
		}finally{
			
			System.out.println("i in finally - from try or catch block is--"+i);
			--i;
			System.out.println("i in finally block is--"+i);
			return --i;
		}
	}


执行结果:

i in try block is--10
i in catch - form try block is--10
i in catch block is--9
i in finally - from try or catch block is--8
i in finally block is--7
the method value is--6

顺序,抛出异常后,执行catch块,在catch块的return的--i执行完后,并不直接返回而是执行finally,因finally中有return语句,所以,执行,返回结果6

情况4,catch中有return,finally中没有,同上,执行完finally语句后,依旧返回catch中的执行return语句后的值,而不是finally中修改的值

情况5:try和catch中都有异常,finally中无return语句

	public static int CatchException(){
		int i=10;
		try{
			System.out.println("i in try block is--"+i);
			i=i/0;
			return --i;
		}catch(Exception e){
			System.out.println("i in catch - form try block is--"+i);
			int j = i/0;
			return --i;
		}finally{
			
			System.out.println("i in finally - from try or catch block is--"+i);
			--i;
			System.out.println("i in finally block is--"+i);
			//return --i;
		}
	}


结果:

i in try block is--10
i in catch - form try block is--10
i in finally - from try or catch block is--10
i in finally block is--9
Exception in thread "main" java.lang.ArithmeticException: / by zero
at exception.ExceptionTest0123.CatchException(ExceptionTest0123.java:29)
at exception.ExceptionTest0123.main(ExceptionTest0123.java:17)

执行顺序:在try块中出现异常,到catch中,执行到异常,到finally中执行,finally执行结束后判断发现异常,抛出

情况6:try,catch中都出现异常,在finally中有返回

	public static int CatchException(){
		int i=10;
		try{
			System.out.println("i in try block is--"+i);
			i=i/0;
			return --i;
		}catch(Exception e){
			System.out.println("i in catch - form try block is--"+i);
			int j = i/0;
			return --i;
		}finally{
			
			System.out.println("i in finally - from try or catch block is--"+i);
			--i;
			System.out.println("i in finally block is--"+i);
			return --i;
		}
	}


运行结果:

i in try block is--10
i in catch - form try block is--10
i in finally - from try or catch block is--10
i in finally block is--9
the method value is--8

执行顺序:try块中出现异常到catch,catch中出现异常到finally,finally中执行到return语句返回,不检查异常

没有catch,只有try和finally时,执行顺序和上面的几种情况差不多,只是少了catch块的执行

欢迎大家补充指正

分享到:
评论

相关推荐

    JSTL详细标签库介绍

    若try抛出Exception,App寻找在Catch1~100寻找合适异常处理程序,若找到,执行CATCH{}代码,没有,执行最后一个catch{}后代码<BR>2、 若try未抛出Exception,就执行执行最后一个catch{}后代码。<BR><BR>3、 throws...

    java面试800题

    Q0045 Java中是怎样捕获异常的? "try { //statement01 } catch(Exception e) { //statement02 } finally { //statement03 }" Q0046 一个文件中是否可以有多个public类? 不可以 Q0047 子类是否可以访问父类的...

    JAVA基础课程讲义

    try, catch,finally ,return 执行顺序 100 异常的处理办法之二,声明异常: throws子句 101 方法重写中声明异常原则 102 异常的处理办法之三,手动抛出异常,throw子句 103 自定义异常 103 使用异常机制建议 104 ...

    java 面试题 总结

    java编译器要求方法必须声明抛出可能发生的非运行时异常,但是并不要求必须声明抛出未被捕获的运行时异常。 6、说出Servlet的生命周期,并说出Servlet和CGI的区别。 Servlet被服务器实例化后,容器运行其init方法,...

    VB.Net常用语法

    一:Try………Catch………finally………end try 捕获错误 把一个可能出错的语句放在try后面,如果出错,执行catch语句, catch可以有多个,第一个catch不能捕获的错误,将被下一个catch语句 所捕获。在所有的处理...

    JAVA面试题最全集

    50.JAVA语言如何进行异常处理,关键字:thorws,throw,try,catch,finally 51.Object类(或者其子类)的finalize()方法在什么情况下被调用? 52.一个“.java”原文件中是否可以包括多个类(不是内部类)? 53.掌握...

    疯狂JAVA讲义

    10.2.1 使用try...catch捕获异常 359 10.2.2 异常类的继承体系 360 10.2.3 访问异常信息 363 10.2.4 使用finally回收资源 364 10.2.5 异常处理的嵌套 367 10.3 Checked异常和Runtime异常体系 367 10.3.1 使用...

    超级有影响力霸气的Java面试题大全文档

    java编译器要求方法必须声明抛出可能发生的非运行时异常,但是并不要求必须声明抛出未被捕获的运行时异常。 9、说出Servlet的生命周期,并说出Servlet和CGI的区别。  Servlet被服务器实例化后,容器运行其init方法...

    mySQL事务处理

    //捕获执行SQL语句组中的异常 } catch (SQLException e) { try { System.out.println("事务执行失败,进行回滚!\n"); con.rollback(); // 若前面某条语句出现异常时,进行回滚,取消前面执行的所有操作 ...

    Java开发技术大全 电子版

    6.6try-catch-finally语句的嵌套231 6.7用户自定义异常234 6.8使用异常来实现键盘输入235 6.9本章小结236 第7章Java的输入和输出237 7.1文件和输入输出流237 7.2InputStream类和OutputStream类的使用238 ...

    VB.net捕获整个网页并保存成图像

    VB.net捕获整个网页并保存成图像有软件 Private Sub GetImage() If WebBrowser1.Document Is Nothing Then Return End If Try Dim scrollWidth As Integer Dim scrollHeight As Integer scrollHeight = ...

    c#学习笔记.txt

    异常处理语句throw, try-catch, try-finally Checked 和 Uncheckedchecked, unchecked fixed 语句Fixed lock 语句Lock (1) foreach 语句为数组或对象集合中的每个元素重复一个嵌入语句组。foreach 语句用于循环访问...

    net学习笔记及其他代码应用

    43.try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后? 答:会执行,在return前执行。 44.两个对象值相同(x.equals(y) == true),但却可有不同...

    C#语言规范(4.0版本)

    3.10 执行顺序 75 4. 类型 77 4.1 值类型 77 4.1.1 System.ValueType 类型 78 4.1.2 默认构造函数 78 4.1.3 结构类型 79 4.1.4 简单类型 79 4.1.5 整型 80 4.1.6 浮点型 81 4.1.7 decimal 类型 82 4.1.8 bool 类型 ...

    SelBlocks:Selenium IDE 的 SelBlocks 扩展

    特征向 Selenese 添加以下控制结构: if , elseIf , else try , catch , finally , throw for , foreach , while , continue , break call , function , return loadJsonVars , loadXmlVars , forJson , for...

    微软C#语言规范,C#语言教程中文版

    3.10 执行顺序 75 4. 类型 77 4.1 值类型 77 4.1.1 System.ValueType 类型 78 4.1.2 默认构造函数 78 4.1.3 结构类型 79 4.1.4 简单类型 79 4.1.5 整型 80 4.1.6 浮点型 81 4.1.7 decimal 类型 82 4.1.8 bool 类型 ...

    C#语言规范4.0

    3.10 执行顺序 75 4. 类型 77 4.1 值类型 77 4.1.1 System.ValueType 类型 78 4.1.2 默认构造函数 78 4.1.3 结构类型 79 4.1.4 简单类型 79 4.1.5 整型 80 4.1.6 浮点型 81 4.1.7 decimal 类型 82 4.1.8 bool 类型 ...

    C#_语言规范_4.0_中文版

    3.10 执行顺序 75 4. 类型 77 4.1 值类型 77 4.1.1 System.ValueType 类型 78 4.1.2 默认构造函数 78 4.1.3 结构类型 79 4.1.4 简单类型 79 4.1.5 整型 80 4.1.6 浮点型 81 4.1.7 decimal 类型 82 4.1.8 bool 类型 ...

    C#语言规范(2.0,3.0,4.0合集)

    3.10 执行顺序 75 4. 类型 77 4.1 值类型 77 4.1.1 System.ValueType 类型 78 4.1.2 默认构造函数 78 4.1.3 结构类型 79 4.1.4 简单类型 79 4.1.5 整型 80 4.1.6 浮点型 81 4.1.7 decimal 类型 82 4.1.8 bool 类型 ...

Global site tag (gtag.js) - Google Analytics