PPT上的一道小题
public static boolean hhh() {
try {
return true;
}finally {
return false;
}
}
public static void main(String []args) {
System.out.println(hhh());
}
执行结果
查阅资料发现,无论try、catch里是否会return,finally块都会执行。
public static boolean hhh() {
public static boolean hhh() {
try {
throw new Exception();
}catch(Exception e){
throw new RuntimeException();
}finally {
return false;
}
}
public static void main(String []args) {
System.out.println(hhh());
}
如果finally块中包含了return语句,即使前面的catch块重新抛出了异常,则调用该方法的语句也不会获得catch块重新抛出的异常,而是会得到finally块的返回值,并且不会捕获异常。
如果在finally中使用return,会warning:finally block does not complete normally
所以最好不要在里面return