java中在finally语句中使用return语句
来源:互联网 发布:淘宝宝贝主图文字添加 编辑:程序博客网 时间:2024/06/06 15:52
当抛出异常时在finally中使用return语句
当一个程序段抛出异常的时候,如果在finally语句中使用了return语句,就会覆盖掉前面try语句中抛出的异常。导致上层方法不能捕获到调用这个方法抛出的异常。
/** * @author Mingming * @Description * @Date Created in 21:00 2017/11/16 * @Modificd By */public class Exception { public static void main(String[] args){ Exception exception = new Exception(); try { int number = exception.test(); }catch (java.lang.Exception e){ System.out.println("main catch Exception"); } } int test() throws java.lang.Exception{ try { throw new java.lang.Exception(); }catch (java.lang.Exception e){ System.out.println("test catch Exception"); throw e; }finally { return 0; } }}
当在try语句中使用了return语句,然后又在finally中使用了return语句
finally语句中的return语句会覆盖掉前面的return语句
/** * @author Mingming * @Description * @Date Created in 21:00 2017/11/16 * @Modificd By */public class Exception { public static void main(String[] args){ Exception exception = new Exception(); try { int number = exception.test(); System.out.println(number); }catch (java.lang.Exception e){ System.out.println("main catch Exception"); } } int test() throws java.lang.Exception{ try { return 2; }catch (java.lang.Exception e){ System.out.println("test catch Exception"); throw e; }finally { return 0; } }}
阅读全文
0 0
- java中在finally语句中使用return语句
- 【火线解码】001.避免在finally语句块中使用return语句
- Java 中 finally块与return语句的返回顺序
- 浅析Java try....catch....finally中return语句执行顺序
- 关于java中finally中的语句在return前还是后执行的个人理解。
- eclipse黄色警告(finally block does not complete normally) ,不建议在finally中使用return语句
- 在using语句中使用return
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含有return语句的执行情况
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含return语句的执行情况总结-编程陷阱
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- 无向图的邻接矩阵,深度优先遍历广度优先遍历的递归与非递归算法
- 常见系统通信协议使用及理解
- 基本的变量类型
- 树的同构
- 训练日记
- java中在finally语句中使用return语句
- javascript不执行的原因
- Linux NFS服务的初步了解
- Python编程从入门到实践:习题6-1~6-3
- 破解myeclipse8.5步骤
- maven setting
- C++括号匹配问题与逆波兰表达式
- 模拟实现memmove函数
- python的第一个库安装pip包工具