return语句如果放在finally里面,会使异常丢失。
见下面一个简单的例子:
package testThrowFinally;
public class TestThrowFinally {
public static void main(String[] args) {
try {
System.out.println(new TestThrowFinally().start());
} catch (Exception e) {
e.printStackTrace();
}
}
public String start() throws Exception{
try {
int a=4/0;
}
finally{
return "hello";
}
}
}
如果你这样写的话,那么程序永远也不会抛出数学异常。
而且可以拿到hello这个串。
具体可以参见Think in java,缺憾:异常丢失。