int num = getNumber();
System.out.println(num);
}
private static int getNumber() {
int a = 10;
try {
a = 20;
System.out.println(a / 0);
} catch (ArithmeticException e) {
a = 30;
return a //此处有return,本应该返回a并跳出方法了,但还有finally;
}
finally {
a = 40; //所以先执行finally,然后再执行catch中的return 返回a为30;
return a; } //但是finally中有return,所以就执行finally中的return
返回值为40, 不执行catch中的return了。
}
}
所以运行结果是40;
int num = getNumber();
System.out.println(num);
}
private static int getNumber() {
int a = 10;
try {
a = 20;
System.out.println(a / 0);
} catch (ArithmeticException e) {
a = 30;
return a //然后此处有return,本应该返回a并跳出方法了,但还有finally
}
finally { //所以先执行finally,然后再执行catch中的return 返回a为30;
a = 40;
}
return a; //此处的returned将不会执行,除非前边没有return语句
(比如将catch中的return语句去掉),那么他才会执行并返回40。
}
}
所以运行结果是30.
|