今天用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程序必须捕获或声明所有的非运行时异常
,但对运行时异常
可以不做处理。
附赠示例代码一个:
=============================================================
2.参考文章:A Tale of Bad Exception Handling in Finally Blocks in Java。
老外开篇第一段文字,写的非常实在。于是我就坚持看完了全文,也找到了这个问题的答案
"There is always something more to learn
. That was the lesson for me last week when I learned something new about the Java programming language, despite having used professionally it for almost 10 years."
- 大小: 3.9 KB
- 大小: 36.1 KB
分享到:
相关推荐
This is the default action if dest does not exist, or if no filesystem is on it -keep-as-directory If one source directory is specified, create a root directory containing that directory, ...
- **Reversing wait() and notify():** Reversing the order of `obj.wait()` and `obj.notify()` might not cause the method to complete normally because `wait()` must be called within a synchronized block ...
does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable ...
标题中的"Normally-open-switch.rar_The Open Road"指的是一个与常开开关相关的程序或文档,可能涉及电子工程或自动化控制领域。常开开关,也称为NO(Normally Open)开关,是指在无外力作用时触点处于断开状态的...
When you insert a CD that would bring up a screen to install the program contained on the CD, this does not happen in this version of Windows, you will have to explore the CD manually and run whatever...
be optional: if the application does not supply it, the square root function must still compute square roots.) These requirements apply to the modified work as a whole. If identifiable sections of ...
be optional: if the application does not supply it, the square root function must still compute square roots.) These requirements apply to the modified work as a whole. If identifiable sections of ...
Compil32 does not do the actual compilation itself it relegates it to ISCmplr.dll. If the ISCmplr project is changed, you normally don t need to recompile Compil32 since it s essentially a text ...
files which do not fit fully into a block, are NOT by default packed into fragments. To illustrate this, a 100K file has an initial 64K block and a 36K remainder. This 36K remainder is not packed into...
`scheduleDate` timestamp NOT NULL default '0000-00-00 00:00:00', `deliverState` int(1) default NULL, `deliverdTime` timestamp NOT NULL default '0000-00-00 00:00:00' ) ENGINE=InnoDB DEFAULT CHARSET...
问题 连接到集群后,JVM不会退出。 我该如何重现? 启动redis集群。 docker run -e "IP=0.0.0.0" -p 7000-7005:7000-7005 grokzen/redis-cluster:latest 然后运行: make test
卡士妥常闭电磁阀Castel_Normally-closed-solenoid-valves_14-E
Assuming the page reference does not result in an access violation, a page fault can be either hard or soft. A hard page fault results in a read from disk, either a page file or memory-mapped file. A...
on their semantics, but does not fully define the semantics. The library also contains built-in functions and exceptions — objects that can be used by all Python code without the need of an import ...
string.sub(s, -3) will return the last 3 bytes of the string which is not necessarily the same as the last three characters of the string, and may or may not be a complete code. You could use the ...
What does not 不连接互联网时,会发生自动退出的情况。 Do not connect to the Internet, will automatically withdraw from the case. What was not tested 把文字转换成swf文件。 Text to swf file. ...
In myeloplastic syndrome the myeloid cells stop developing they do not function normally and either die in the bone marrow or soon after they enter the blood. The dysfunctional cells crowd out ...
At a minimum, you will need a breadboard and two digital multimeters, but normally you will also need an oscilloscope and a signal generator. Two of these experiments are called "optional" and are in...
集合了 所有的 Unix命令大全 ...telnet 192.168.0.23 自己帐号 sd08077-you0 ftp工具 192.168.0.202 tools-toolss ... 各个 shell 可互相切换 ksh:$ sh:$ csh:guangzhou% bash:bash-3.00$ ... 命令和参数之间必需用空格隔...
The Class Library Reference usually does not describe inherited member functions, inherited operators, and overridden virtual member functions. For information on these functions, refer to the base ...