package cn.yunhe.test;
public class Test1 {
public static void main(String[] args) {
System.out.println(show1());
System.out.println(show2());
System.out.println(show3());
show4();
}
@SuppressWarnings("finally")
public static int show1() {
try {
return 1;
} catch (Exception e) {
} finally {
System.out.println("show1");
return -1;
}
}
@SuppressWarnings("finally")
public static int show2() {
try {
} catch (Exception e) {
return 2;
} finally {
System.out.println("show2");
return -2;
}
}
//当finally有返回值时,会直接返回。不会再去返回try或者catch中的返回值
public static int show3() {
int result = 0;
try {
return result;
} finally {
System.out.println("show3");
result= -3;
}
}
//并不会改变返回的内容。
//当返回的变量的类型是引用类型时,后面finally中语句即使有对返回的变量进行赋值的操作时,也不会影响返回的值
public static void show4() {
try {
} catch (Exception e) {
System.exit(4);
} finally {
System.out.println("show4");
}
}
}
return和finally的执行顺序
最新推荐文章于 2021-06-06 08:29:33 发布