Java–异常/Exception–try/catch/finally的return顺序

Java中捕获异常时,是否return对程序流程的影响。


一、正常用法(try异常, catch/finally无return)

示例:

package org.example.a;
public class Demo {
    public static void main(String[] args) {
        Object handler = handler();
        System.out.println(handler.toString());
    }
    public static Object handler() {
        try {
            System.out.println("try:内(前)");
            System.out.println("try:内(异常)" + 5 / 0);
            System.out.println("try:内(后)");
            return "try:返回";
        } catch (Exception e) {
            System.out.println("catch:内(前)");
//            System.out.println("catch:内(异常)" + 5 / 0);
//            System.out.println("catch:内(后)");
//            return "catch:返回";
        } finally {
            System.out.println("finally:内");
//            return "finally:返回";
        }
        System.out.println("最后");
        return "最后(返回)";
    }
}

执行结果:(try=> catch=> finally=> finally块之外)

try:内(前)
catch:内(前)
finally:内
最后
最后(返回)

二、try无异常, try有return, finally无return

示例:

package org.example.a;
public class Demo {
    public static void main(String[] args) {
        Object handler = handler();
        System.out.println(handler.toString());
    }
    public static Object handler() {
        try {
            System.out.println("try:内(前)");
//            System.out.println("try:内(异常)" + 5 / 0);
//            System.out.println("try:内(后)");
            return "try:返回";
        } catch (Exception e) {
            System.out.println("catch:内(前)");
            System.out.println("catch:内(异常)" + 5 / 0);
            System.out.println("catch:内(后)");
            return "catch:返回";
        } finally {
            System.out.println("finally:内");
//            return "finally:返回";
        }
    }
}

执行结果(try=> finally=> try的return)

try:内(前)
finally:内
try:返回

三 try无异常, try有return,finally有return(Idea报警告)

示例:

package org.example.a;
public class Demo {
    public static void main(String[] args) {
        Object handler = handler();
        System.out.println(handler.toString());
    }
    public static Object handler() {
        try {
            System.out.println("try:内(前)");
//            System.out.println("try:内(异常)" + 5 / 0);
//            System.out.println("try:内(后)");
            return "try:返回";
        } catch (Exception e) {
            System.out.println("catch:内(前)");
            System.out.println("catch:内(异常)" + 5 / 0);
            System.out.println("catch:内(后)");
            return "catch:返回";
        } finally {
            System.out.println("finally:内");
            return "finally:返回";
        }
    }
}

执行结果:执行结果(try=> finally=> 程序结束(不调用try的return))

try:内(前)
finally:内
finally:返回

四 try有异常, catch有return, finally无return

package org.example.a;
public class Demo {
    public static void main(String[] args) {
        Object handler = handler();
        System.out.println(handler.toString());
    }
    public static Object handler() {
        try {
            System.out.println("try:内(前)");
            System.out.println("try:内(异常)" + 5 / 0);
            System.out.println("try:内(后)");
            return "try:返回";
        } catch (Exception e) {
            System.out.println("catch:内(前)");
//            System.out.println("catch:内(异常)" + 5 / 0);
//            System.out.println("catch:内(后)");
            return "catch:返回";
        } finally {
            System.out.println("finally:内");
//            return "finally:返回";
        }
    }
}

执行结果:执行结果(try=> catch=> finally=> catch的return)

try:内(前)
catch:内(前)
finally:内
catch:返回

五 try有异常, catch有return, finally有return

示例:

package org.example.a;
public class Demo {
    public static void main(String[] args) {
        Object handler = handler();
        System.out.println(handler.toString());
    }
    public static Object handler() {
        try {
            System.out.println("try:内(前)");
            System.out.println("try:内(异常)" + 5 / 0);
            System.out.println("try:内(后)");
            return "try:返回";
        } catch (Exception e) {
            System.out.println("catch:内(前)");
//            System.out.println("catch:内(异常)" + 5 / 0);
//            System.out.println("catch:内(后)");
            return "catch:返回";
        } finally {
            System.out.println("finally:内");
            return "finally:返回";
        }
    }
}

执行结果:执行结果(try=> catch=> finally=> 程序退出)

try:内(前)
catch:内(前)
finally:内
finally:返回

六 try有异常, catch有异常, finally无return

示例:

package org.example.a;
public class Demo {
    public static void main(String[] args) {
        Object handler = handler();
        System.out.println(handler.toString());
    }
    public static Object handler() {
        try {
            System.out.println("try:内(前)");
            System.out.println("try:内(异常)" + 5 / 0);
            System.out.println("try:内(后)");
            return "try:返回";
        } catch (Exception e) {
            System.out.println("catch:内(前)");
            System.out.println("catch:内(异常)" + 5 / 0);
            System.out.println("catch:内(后)");
            return "catch:返回";
        } finally {
            System.out.println("finally:内");
//            return "finally:返回";
        }
    }
}

执行结果;执行结果(try=> catch=> finally=> 程序结束(catch不会再return))

Exception in thread "main" java.lang.ArithmeticException: / by zero
	at org.example.a.Demo.handler(Demo.java:18)
	at org.example.a.Demo.main(Demo.java:5)
try:内(前)
catch:内(前)
finally:内

总结

先看try里面有无异常,有的话进入catch再执行catch逻辑,finally是必须执行的

  • 若try里面无异常,此时不进入catch,当try里面有return时,先于return执行finally中语句,当此时finall中无rerurn时,执行完finally语句再执行try中的return,当此时finally中有return时,则return执行finally中的,不执行try中的return,程序结束。
  • 若try里面有异常,则进入catch,执行catch中语句,看catch里面是否有return,遇到return先执行finally,若finally中也有return则跳过catch中的return,需要注意:若执行的catch中也有异常,则执行到异常处直接执行finally,catch中处于异常后的语句都不执行(包含return)。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值