所有分类
  • 所有分类
  • 未分类

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

简介

简介

本文介绍Java中捕获异常时是否return对程序流程的影响。

总结

  1. 如果catch或者finally中有return,则catch和finally代码块之后的部分根本不会执行到。
  2. 如果catch和finally中都有return,后边的(也就是finally的)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无异常, 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=> finally=> try的return)

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

try无异常, 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:内
2

评论2

请先

  1. try无异常, catch有return, finally无return 这个例子里会最后跑到try里面真没想到
    哥德温 2024-04-09 1
显示验证码
没有账号?注册  忘记密码?

社交账号快速登录