zoukankan      html  css  js  c++  java
  • Java异常处理中finally中的return会覆盖catch语句中的return语句

    Java异常处理中finally中的return会覆盖catch语句中的return语句和throw语句,所以Java不建议在finally中使用return语句

    此外 finally中的throw语句也会覆盖catch语句中的return语句和throw语句

    程序实例如下:(本代码来源于CSDN某大神:http://blog.csdn.net/hguisu/article/details/6155636   在此表示感谢)

    package Test;  
      
    public class TestException {  
        public TestException() {  
        }  
      
        boolean testEx() throws Exception {  
            boolean ret = true;  
            try {  
                ret = testEx1();  
            } catch (Exception e) {  
                System.out.println("testEx, catch exception");  
                ret = false;  
                throw e;  
            } finally {  
                System.out.println("testEx, finally; return value=" + ret);  
                return ret;  
            }  
        }  
      
        boolean testEx1() throws Exception {  
            boolean ret = true;  
            try {  
                ret = testEx2();  
                if (!ret) {  
                    return false;  
                }  
                System.out.println("testEx1, at the end of try");  
                return ret;  
            } catch (Exception e) {  
                System.out.println("testEx1, catch exception");  
                ret = false;  
                throw e;  
            } finally {  
                System.out.println("testEx1, finally; return value=" + ret);  
                return ret;  
            }  
        }  
      
        boolean testEx2() throws Exception {  
            boolean ret = true;  
            try {  
                int b = 12;  
                int c;  
                for (int i = 2; i >= -2; i--) {  
                    c = b / i;  
                    System.out.println("i=" + i);  
                }  
                return true;  
            } catch (Exception e) {  
                System.out.println("testEx2, catch exception");  
               ret = false;  
                throw e;  
            } finally {  
                System.out.println("testEx2, finally; return value=" + ret);  
                return ret;  
            }  
        }  
          public static void main(String[] args) {  
            TestException testException1 = new TestException();  
            try {  
                testException1.testEx();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
    }  

    输出结果为:

    i=2
    i=1
    testEx2, catch exception
    testEx2, finally; return value=false
    testEx1, finally; return value=false
    testEx, finally; return value=false

    此外:

    finally块的语句在try或catch中的return语句执行之后返回之前执行且finally里的修改语句不能影响try或catch中 return已经确定的返回值(如果catch语句中返回的是一个引用,那么在finally语句中对引用的内容进行修改是会影响catch中return的返回值的,若finally里也有return语句则覆盖try或catch中的return语句直接返回。

  • 相关阅读:
    巩固基础前台
    super 、static、final关键字加深记忆哦!还有父子类构造函数调用问题
    java异常了解
    spring粗略整体认识
    java枚举新认识
    泛型集合注意事项
    java反射基础
    对java集合类的认识——基础很重要
    多线程(C++ And POSIX)
    v2代理原理,应用
  • 原文地址:https://www.cnblogs.com/qingergege/p/5716377.html
Copyright © 2011-2022 走看看