• 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

  • 相关阅读:
    Bootstrap背景色设置相关
    【linux基础(1)】
    操作系统学习——第一类R/W问题和第二类R/W问题
    Dynamsoft Barcode Reader新框架将医疗视觉提升到新水平
    大数据必学Java基础(九十):通过反射获取运行时类的完整结构
    【C++】string类接口的了解和使用
    Python 写网络监控
    机器学习——终身学习
    Springboot出现@Value注解无法读取配置文件的解决方法
    CPU是海王?聊聊 主/子线程 和 同/异步 的关系
  • 原文地址:https://blog.csdn.net/u011164827/article/details/132826835