• 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

  • 相关阅读:
    如何使用KVM 虚拟机安装RHEL7系统
    KVM虚拟机安装报错 KVM is not available
    Delphi读取不Word中不规则表格数据并转换成标准表格
    Delphi控制Excel输出上标示例
    动态设置和访问cxgrid列的Properties(转)
    PHP存储blob示例(转)
    visual studio code(vscode) 调试php(转)
    Node入门(转)
    ThinkPHP升级指导
    微信官方demo(php)
  • 原文地址:https://www.cnblogs.com/wyisprogramming/p/6610950.html
Copyright © 2020-2023  润新知