代码示例
/**
* @program: java_base
* @description:
* @author: ChenWenLong
* @create: 2019-11-14 16:46
**/
public class TestReturnAndFinally {
public static void main(String[] args) {
System.out.println(testReturn());// 1
System.out.println(testFinally());// 1
System.out.println(test());// 1
System.out.println(testAdd());// 2
}
public static int testReturn(){
int x = 1;
try {
return 1;
}finally {
x = 2;
}
}
public static int testFinally(){
int x = 1;
try {
return x;
}finally {
return 2;
}
}
public static int testAdd(){
int x = 1;
try {
return x;
}finally {
int y = ++x;
return y;
}
}
public static int test(){
int x = 1;
try {
return x;
}finally {
int y = x++;
return y;
}
}
}