• Java异常try{}catch{}中的return机制


    try catch 和方法中都有返回

    public class MainTest {
        public static void main(String[] args) {
            System.out.println("测试结果 " + test(true));
        }
    
        static String test(boolean exception){
            try {
                System.out.println("start");
                if(exception){
                    throw new Exception(new RuntimeException());
                }
                return "try";
            } catch (Exception e){
                System.out.println("Exception");
                e.printStackTrace();
                return "Exception";
            } finally {
                System.out.println("finally");
                //return "finally";
            }
            return "test";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    不允许这样写,运行时会报“错误: 无法访问的语句 return “test”;
    当try 和catch 中都有返回时,方法是不允许有return,因为代码不会执行到try catch之外。

    try 和方法中都有返回

    public class MainTest {
        public static void main(String[] args) {
            System.out.println("测试结果 " + test(true));
        }
    
        static String test(boolean exception){
            try {
                System.out.println("start");
                if(exception){
                    throw new Exception(new RuntimeException());
                }
                return "try";
            } catch (Exception e){
                System.out.println("Exception");
                e.printStackTrace();
               // return "Exception";
            } finally {
                System.out.println("finally");
                //return "finally";
            }
            System.out.println("最后一行");
            return "finish";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    结果

    start
    Exception
    finally
    最后一行
    测试结果 finish
    java.lang.Exception: java.lang.RuntimeException
    	at com.example.testdemo.MainTest.test(MainTest.java:12)
    	at com.example.testdemo.MainTest.main(MainTest.java:5)
    Caused by: java.lang.RuntimeException
    	... 2 more
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    1.方法执行完毕后抛出异常
    2.先进入到catch中,然后是finally,最后执行方法return

    finally和方法中都有return

    public class MainTest {
        public static void main(String[] args) {
            System.out.println("测试结果 " + test(true));
        }
    
        static String test(boolean exception){
            try {
                System.out.println("start");
                if(exception){
                    throw new Exception(new RuntimeException());
                }
               //return "try";
            } catch (Exception e){
                System.out.println("Exception");
                e.printStackTrace();
                //return "Exception";
            } finally {
                System.out.println("finally");
                return "finally";
            }
            System.out.println("最后一行");
            return "finish";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    无法执行,因为finally一定会被执行,方法的return无法执行

    try catch 和finally中都有return

    public class MainTest {
        public static void main(String[] args) {
            System.out.println("测试结果 " + test(false));
        }
    
        static String test(boolean exception){
            try {
                System.out.println("start");
                if(exception){
                    throw new Exception(new RuntimeException());
                }
               return "try";
            } catch (Exception e){
                System.out.println("Exception");
                e.printStackTrace();
                return "Exception";
            } finally {
                System.out.println("finally");
                return "finally";
            }
    //        System.out.println("最后一行");
    //        return "finish";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    start
    finally
    测试结果 finally
    
    • 1
    • 2
    • 3

    最终执行finally中的return

  • 相关阅读:
    541、RabbitMQ详细入门教程系列 -【Jackson2JsonMessageConvert】 2022.09.05
    Java中的String数据类型,String类(字符串)详解
    云计算模式的优势
    @Api注解
    推荐一款基于 .NET Core开源的小程序商城系统
    个人记账本,教你使用图表格查看项目
    Linux:文件搜索
    CV经典任务(一) 语义分割、实例分割 | 全卷积
    2D物理系统——物理材质 & 恒定力
    Java,给数组元素赋不同的随机值
  • 原文地址:https://blog.csdn.net/u011164827/article/details/132826835