深入了解try-catch-finally
1.下列代码的运行结果是:
public class TryTest01 {
static int test(){
int x=1;
try {
return x;
}finally{
++x;
}
}
public static void main(String[] args) {
System.out.println(test());
}
}
2.下列代码的运行结果是:
public class TryTest02 {
public static void main(String[] args) {
System.out.println(new TryTest02().get());
}
static int get() {
try {
return 1;
} finally {
return 2;
}
}
}
3.下列代码的运行结果是:
public class TryTest03 {
static int get(){
int i=3;
try {
return i;
} finally{
System.out.println("aa");
}
}
public static void main(String[] args) {
System.out.println("结果是:"+newTryTest03().get());
}
}
运行结果:
(1)1 (2)2
(3)aa
结果是:3
----------------------------------------------------------------
本内容由安康学院"雨季"原创!