java 中 finally里面写了return 会发生什么?
2016-01-25 20:29
567 查看
boolean test() throws Exception { boolean ret = true; try { int b = 12; int c; for (int i = 1; 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; } }
这是一种很不好的写法。
开始认为会输出
i=12
testEx2, catch exception
testEx2, finally; return value=false 但事实是
i =12
testEx2, finally; return value=false 原因是finally里写return return会冲掉上边的return 并且干掉catch抛出的异常
相关文章推荐
- Log4j2 与 SpringMVC 整合
- Java基础之----I/O
- The Java™ Tutorials — Generics :Generic Types 泛型
- JAVA观感器
- 简述java构造函数
- java引用数据类型之数组
- java开发环境的搭配
- java常用关键字
- java标识符要点
- Eclipse报错-->failed to load the jni shared library
- 虚拟机字节码执行引擎
- Java 多线程问题例子
- 用Jersey构建RESTful服务7--Jersey+SQLServer+Hibernate4.3+Spring3.2
- Java反射机制(2)
- Elasticsearch的javaAPI之percolator
- Struts2之文件上传
- The Java™ Tutorials — Generics :Why Use Generics? 为什么使用泛型
- 1,Java的移位操作效果; 2,移位如何实施的; 3, 原码补码反码原理
- 使用Sublime Text 3 编译并运行Java程序
- 使用spring websocket stomp遇到的3个问题总结:路径匹配/自动注入/activemq连接超时