“finally block does not complete normally”的警告
来源:互联网 发布:淘宝客服新人自我介绍 编辑:程序博客网 时间:2024/06/06 20:00
转自:http://sw1982.iteye.com/blog/773331
今天用try-finally的时候,整出一个“finally block does not complete normally”的警告。想了半天也没明白什么一个情况。
可以看下面的代码, return 写在finally块就会得到警告!解决方案: return放到finally外面。
但是,java里面不是可以保证finally一定会执行的么,为什么不可以在finally块做return???
细细看道来:
debug一下这个函数,就会惊讶的发现, 里面抛出的异常会被finally吃掉。 这也就是为什么会被警告的原因。
@SuppressWarnings("finally") private boolean isReturnWithinFinally() { try { if (true) throw new RuntimeException(); } finally { return true; // This hides the exception } }
那么,下面这样会不会ok呢?先把异常处理
public static void main(String[] args) {try{throw new RuntimeException();}catch(Exception e) {//}finally {return;}}
结论是:依旧不行。java里面的异常分为可不获和不可捕获两类,即便使用到catch块,也会导致非捕获的错误被finally吃掉。
因此,return一定要放到finally外面。
===1.java异常简介
类Throwable有两个直接子类:Error和Exception。Error类对象(如动态连接错误等),由Java虚拟机生成并抛弃(通常,Java程序不对这类 异常进行处理) ;Exception类对象是Java程序处理或抛弃的对象。它有各种不同的子类分别对应于不同类型的例外。其中类RuntimeException代表运行时由Java虚拟机生成的 异常,如算术运算异常ArithmeticException(由除 0错等导致)、数组越界例外ArrayIndexOutOfBoundsException等;其它则为非运行时异常,如输入输出例外 IOException等。Java编译器要求Java程序必须捕获或声明所有的非运行时异常 ,但对运行时异常 可以不做处理。
附赠示例代码一个:
- “finally block does not complete normally”的警告
- "finally block does not complete normally"警告解决
- finally块的问题(finally block does not complete normally)
- finally块的问题(finally block does not complete normally)
- finally块的问题(finally block does not complete normally)
- finally block does not complete normally
- 错误分析:finally block does not complete normally
- 错误分析:finally block does not complete normally
- 错误分析:finally block does not complete normally
- eclipse黄色警告(finally block does not complete normally) ,不建议在finally中使用return语句
- IDE 提示 'finally' block can not complete normally的问题
- Java中finally块报finally block does not complete normally
- eclipse中 “Copy" did not complete normally.错误的真正原因。
- "Copy" did not complete normally. Please see the log for more information.
- "Copy" did not complete normally. Please see the log for more information
- Copy" did not complete normally. Please see the log for more information
- android 老是弹出 "Copy" did not complete normally. Please see the log for more information.
- "Format" did not complete normally。MyEclipse手动格式化web.xml文件错误
- C语言练习
- 转帖:DirectShow 在VS2005中环境配置
- poj 1269 Intersecting Lines
- 委托与事件以及应用
- USB协议枚举
- “finally block does not complete normally”的警告
- jvm 系列之一 参数配置
- easyui datagrid中添加右键菜单事件
- http dynamic streaming
- Tomcat启动错误
- PHP生成柱状图、饼状图和线性图
- 第一格一粒米,后一格为前一格的两倍,共有十六格,求总数
- db1
- (转)FLV文件结构解析