try/catch/finally的各种情况

众所周知,try语句报错,会执行catch语句,然后执行finally,以下这几种情况,看看会如何输出。

1、try语句中包含return,finally包含输出语句

public static void main(String[] args) {
	// write your code here
        System.out.println(get());
}

public static String get(){
    try {
        return "111";
    } catch (Exception e) {
        throw new NullPointerException();
    } finally {
        System.out.println(123);
    }
 }

结果:finally正常输出

 2、try语句报错,catch语句抛出错误,finally包含输出语句

public static void main(String[] args) {
	// write your code here
        System.out.println(get());
}

public static String get(){
   try {
    	throw new NullPointerException();
	} catch (Exception e) {
 		throw new NullPointerException();
	} finally {
		System.out.println(123);
    }
}

结果:finally执行成功,再抛出catch中的错误

 3、try语句报错,catch包含return,finally包含输出语句

public static void main(String[] args) {
	// write your code here
        System.out.println(get());
}

public static String get(){
    try {
        throw new NullPointerException();
    } catch (Exception e) {
        System.out.println(111);
        return "456";
    } finally {
        System.out.println(123);
    }
}

结果:catch中的语句输出成功,finally中的语句输出成功

 4、try和finally中的语句都包含输出

public static void main(String[] args) {
	// write your code here
    System.out.println(get());
}

public static String get(){
    try {
        return "123";
    } catch (Exception e) {
        System.out.println(111);
        return "456";
    } finally {
        return "789";
    }
}

结果:finally成功return

 5、try报错,catch和finally包含return语句

public static void main(String[] args) {
	// write your code here
    System.out.println(get());
}

public static String get(){
    try {
        throw new NullPointerException();
    } catch (Exception e) {
        System.out.println(111);
        return "456";
    } finally {
        return "789";
    }
}

结果:finally成功return

 6、try包含return,finally抛出错误

    public static void main(String[] args) {
	// write your code here
        System.out.println(get());
    }

    public static String get(){
        try {
            return "789";

        } catch (Exception e) {
            System.out.println(111);
            return "456";
        } finally {
            throw new NullPointerException();
        }
    }

结果:直接报错

 7、try包含报错,catch包含return,finally抛出错误

public static void main(String[] args) {
	// write your code here
    System.out.println(get());
}

public static String get(){
    try {
        throw new RuntimeException();
    } catch (Exception e) {
        return "456";
    } finally {
        throw new NullPointerException();
    }
}

结果:直接报错

通过上面的例子,我们可以得到以下结论:

  1. finally语句存在的话,一定会执行
  2. 若try语句报错,会执行catch语句,如果catch语句也报错,则会先执行finally语句,再抛出catch的错误
  3. try语句或者catch语句return了,finally仍然会执行
  4. 若finally语句中包含return,则会覆盖try或者catch中的return
  5. 若finally语句报错,这个错误一定会抛出,终止程序
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

☆叙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值