前言
最近遇到一个try->catch->finally->return
问题的变式,由于只知道是这么个顺序,但不知其原理,所以问题稍微变下就答不上来了。现在从字节码角度来分析这个问题。
代码
Sample1
public class TryCatchTest {
public static void main(String[] args) {
System.out.println(finallyTest());
}
public static int finallyTest() {
int a;
try {
a = 1;
if (a == 1) {
throw new RuntimeException("deliberate...");
}
return a;
} catch (RuntimeException e) {
a = 2;
if (a == 2) {
throw new RuntimeException("deliberate1...");
}
return a;
} finally {
a = 3;
return a;
}
}
}
Sample2
public class TryCatchTest {
public static void main(String[] args) {
System.out.println(finallyTest());
}
public static int finallyTest() {
int a;
try {
a = 1;
if (a == 1) {
throw new RuntimeException("deliberate...");
}
return a;