博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA异常处理之finally中最好不要使用return
阅读量:2069 次
发布时间:2019-04-29

本文共 2938 字,大约阅读时间需要 9 分钟。

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 }

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

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) {        }    }}

输出结果为5, 5

 

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

转载地址:http://eccmf.baihongyu.com/

你可能感兴趣的文章
用 LSTM 来做一个分类小问题
查看>>
详解 LSTM
查看>>
按时间轴简述九大卷积神经网络
查看>>
详解循环神经网络(Recurrent Neural Network)
查看>>
为什么要用交叉验证
查看>>
用学习曲线 learning curve 来判别过拟合问题
查看>>
用验证曲线 validation curve 选择超参数
查看>>
用 Grid Search 对 SVM 进行调参
查看>>
用 Pipeline 将训练集参数重复应用到测试集
查看>>
PCA 的数学原理和可视化效果
查看>>
机器学习中常用评估指标汇总
查看>>
什么是 ROC AUC
查看>>
Bagging 简述
查看>>
详解 Stacking 的 python 实现
查看>>
简述极大似然估计
查看>>
用线性判别分析 LDA 降维
查看>>
用 Doc2Vec 得到文档/段落/句子的向量表达
查看>>
使聊天机器人具有个性
查看>>
使聊天机器人的对话更有营养
查看>>
一个 tflearn 情感分析小例子
查看>>