zoukankan      html  css  js  c++  java
  • JAVA异常处理之finally中最好不要使用return

    finally 语句块中, 最好不要使用return, 否则会造成已下后果;

    1, 如果catch块中捕获了异常, 并且在catch块中将该异常throw给上级调用者进行处理, 但finally中return了, 那么catch块中的throw就失效了, 上级方法调用者是捕获不到异常的. 见demo如下:

     1 public class TestException {
     2     public TestException() {
     3     }
     4 
     5     boolean testEx() throws Exception {
     6         boolean ret = true;
     7         try {
     8             ret = testEx1();
     9         } catch (Exception e) {
    10             System.out.println("testEx, catch exception");
    11             ret = false;
    12             throw e;
    13         } finally {
    14             System.out.println("testEx, finally; return value=" + ret);
    15             return ret;
    16         }
    17     }
    18 
    19     boolean testEx1() throws Exception {
    20         boolean ret = true;
    21         try {
    22             ret = testEx2();
    23             if (!ret) {
    24                 return false;
    25             }
    26             System.out.println("testEx1, at the end of try");
    27             return ret;
    28         } catch (Exception e) {
    29             System.out.println("testEx1, catch exception");
    30             ret = false;
    31             throw e;
    32         } finally {
    33             System.out.println("testEx1, finally; return value=" + ret);
    34             return ret;
    35         }
    36     }
    37 
    38     boolean testEx2() throws Exception {
    39         boolean ret = true;
    40         try {
    41             int b = 12;
    42             int c;
    43             for (int i = 2; i >= -2; i--) {
    44                 c = b / i;
    45                 System.out.println("i=" + i);
    46             }
    47             return true;
    48         } catch (Exception e) {
    49             System.out.println("testEx2, catch exception");
    50             ret = false;
    51             throw e;
    52         } finally {
    53             System.out.println("testEx2, finally; return value=" + ret);
    54             return ret;
    55         }
    56     }
    57 
    58     public static void main(String[] args) {
    59         TestException testException1 = new TestException();
    60         try {
    61             testException1.testEx();
    62         } catch (Exception e) {
    63             e.printStackTrace();
    64         }
    65     }
    66 }
    View Code

    该方法的最终执行结果如下:

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

    尽管testEx2中的catch抛出异常, 但因为finally中return了, 那么testEx1中是捕获不到异常的, 所以testEx1中的catch块是不会执行的.

    2, 如果在finally里的return之前执行了其它return , 那么最终的返回值是finally中的return:

    public class TestException3 {
        public static int x = 3;
        int testEx() throws Exception {
            try {
                x = 4;
                return x;
            } catch (Exception e) {
    
            } finally {
                x = 5;
                return x;
            }
        }
        public static void main(String[] args) {
            TestException3 testException3 = new TestException3();
            try {
                int a = testException3.testEx();
                System.out.println(a);
                System.out.println(x);
            } catch (Exception e) {
            }
        }
    }
    View Code

    输出结果为5, 5

    如果finally中的return被注释掉, 那么输出结果是4, 5

  • 相关阅读:
    Yii2 的 updateAll 方法参数详解
    Yii2 数据库查询汇总
    Git常见报错
    git rebase篇
    在 Yii 2.0 上,使用 updateAll() 更新表列值为同一表的另一列值的实现
    Git从其他分支merge个别文件
    Git cherry-pick 复制多个commit
    Git 删除某次提交(某个commit)的方法
    多进程记录
    python3安装Crypto过程
  • 原文地址:https://www.cnblogs.com/wyisprogramming/p/6610950.html
Copyright © 2011-2022 走看看