下面函数将返回?
public static int func (){
try{
return 1;
}catch (Exception e){
return 2;
}finally{
return 3;
}
}
int i = 0 ;
int test(){
try {
return i;
} catch (Exception e) {
return -1;
}finally{
i++;
}
}
//input: 0
static int test() {
int i = 0;
try {
i += 1;// 1
return i;// 被finally拦截
} catch (Exception e) {
return -1;
} finally {
i += 2;// 3
return i;// 3
}
}
输出结果为:3 try块的return被拦截
static int test() {
int i = 0;
try {
i += 1;// i=1
return i;// 将返回之前的值放入temp=1,执行完finally后,返回temp值(此时i=3,temp=1)
} catch (Exception e) {
return -1;
} finally {
i += 2;// i=3
}
}
输出结果:1 return返回的是一个临时储存的变量temp而不是i了