java throw return_java异常处理-finally中使用return和throw语句

java异常语句中的finally块通常用来做资源释放操作,如关闭文件、关闭网络连接、关闭数据库连接等。正常情况下finally语句中不应该使用return语句也不应该抛出异常,以下讨论仅限于java语言设计本身,正常编码时应避免。

finally块中使用return会覆盖method的返回值

以下代码的返回值为:1

public static intdiv(){try{return 3;

}catch(ArithmeticException e){

System.out.println("catch in div");return 2;

}finally{

System.out.println("finally in div");return 1;

}

}

以下代码的返回值同样是:1

public static intdiv(){try{return 3/0;

}catch(ArithmeticException e){

System.out.println("catch in div");return 2;

}finally{

System.out.println("finally in div");return 1;

}

}

finally块中使用return会抑制异常的冒泡传输

即:只要finally中使用了return语句,调用者便认为该方法正常返回

以下代码的输出为

catch in div

finally in div

catch in adapter

finally in adapter

/**

* Created by Administrator on 2017/11/27.*/

public classTest {public static voidmain(String[] args) {

adapter();

}public static voidadapter() {try{

div();

}catch(ArithmeticException e) {

System.out.println("catch in adapter");

}finally{

System.out.println("finally in adapter");

}

}public static intdiv() {try{int a = 5 / 0;returna;

}catch(ArithmeticException e) {

System.out.println("catch in div");throw e; //重新将异常抛出给调用者

} finally{

System.out.println("finally in div");

}

}

}

但如果在 div 的finally块中添加了return语句

public static intdiv(){try{int a = 5/0;returna;

}catch(ArithmeticException e){

System.out.println("catch in div");throwe; // 重新将异常抛出给调用者,但是抛出会被忽略

}finally{

System.out.println("finally in div");return 1;

}

}

则代码的输出为

catch in div

finally in div

finally in adapter

即:finally块中的return语句会阻止异常的栈调用传输,使caller认为该方法已经正常返回

finally块中的throw语句会覆盖try和catch语句中的异常

以下代码

/**

* Created by Administrator on 2017/11/27.*/

public classTest {public static voidmain(String[] args) {

adapter();

}public static voidadapter() {try{

div();

}catch(Exception e) {

System.out.println(String.format("catch in adapter: %s",e.getMessage()));

}finally{

System.out.println("finally in adapter");

}

}public static intdiv() throws Exception{try{int a = 5 / 0;returna;

}catch(ArithmeticException e) {

System.out.println("catch in div");throw new Exception("Exception in div"); //抛出新的异常

} finally{

System.out.println("finally in div");throw new Exception("Exception in Finally"); //抛出新的异常

}

}

}

输出是:

catch in div

finally in div

catch in adapter: Exception in Finally

finally in adapter

即,catch块中抛出的异常北finally块抛出的异常替换了

修改div方法为

public static intdiv() throws Exception{try{int a = 5 / 0;returna;

}finally{

System.out.println("finally in div");throw new Exception("Exception in Finally"); //抛出新的异常

}

}

输出为:

finally in div

catch in adapter: Exception in Finally

finally in adapter

即,try块中捕获的异常北finally块抛出的异常替换

finally块和普通代码块一样,无法同时使用return语句和throw语句,因为无法通过编译

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值