对于 try 和 finally 至少一个语句块包含 return 语句的情况:
如下面的例子
public static void main(String[] args) {
System.out.println(getString());
}
public static String getString() {
String str = "A";
try {
str = "B";
return str;
} finally {
System.out.println("finally change return string to C");
str = "C";
// return str;
}
}
打印
finally change return string to C
B
finally 语句块中新增 return 语句
public static void main(String[] args) {
System.out.println(getString());
}
public static String getString() {
String str = "A";
try {
str = "B";
return str;
} finally {
System.out.println("finally change return string to C");
str = "C";
return str;
}
}
打印结果
finally change return string to C
C
ConstXiong 备案号:苏ICP备16009629号-3