现在的位置: 首页 > 综合 > 正文

JAVA try…catch…finally中的return语句

2012年08月28日 ⁄ 综合 ⁄ 共 650字 ⁄ 字号 评论关闭
public static int get() {
try {
System.out.println(
"try");
return 1;
//throw new Exception();
} catch (Exception e) {
System.out.println(
"catch");
return 2;
}
finally {
System.out.println(
"finally");
return 3;
}
}
结果:
try
finally
3
public static int get() {
try {
System.out.println(
"try");
//return 1;
throw new Exception();
}
catch (Exception e) {
System.out.println(
"catch");
return 2;
}
finally {
System.out.println(
"finally");
return 3;
}
}
结果:
try
catch
finally
3
public static int get() {
try {
System.out.println(
"try");
//return 1;
throw new Exception();
}
catch (Exception e) {
System.out.println(
"catch");
return 2;
}
finally {
System.out.println(
"finally");
//return 3;
}
}
结果:
try
catch
finally
2
结论:finally块中的return会覆盖掉try或catch块中的return

抱歉!评论已关闭.