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
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

相关文章:

  • 2021-10-30
  • 2022-01-24
  • 2022-12-23
  • 2021-09-27
  • 2022-12-23
猜你喜欢
  • 2021-09-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-17
  • 2022-12-23
  • 2021-05-30
相关资源
相似解决方案