public static void main(String[] args) {
int test = test();
System.out.println("test = " + test);
}
public static int test(){
try {
int i =1/0;
return i;
} catch (Exception e) {
return getNum(1);
}finally {
return getNum(2);
}
}
public static int getNum(int num){
System.out.println("执行getNum:"+num);
return num;
}
执行结果
执行getNum:1
执行getNum:2
test = 2
说明finally一定会执行,会在什么时候执行,在try或者catch的return跳出方法前执行。