public class Test {
public static int testInt() {
int x = 0;
try {
x = 1;
System.out.println("x=1");
return x;
}
finally {
x = 2;
System.out.println("x=2");
}
}
public static Ref testRef() {
Ref ref = new Ref();
try {
ref.x = 1;
System.out.println("x=1");
return ref;
}
finally {
ref.x = 2;
System.out.println("x=2");
}
}
public static void main(String[] args) {
System.out.println("x=? " + testInt());
System.out.println("x=? " + testRef());
}
private static class Ref {
private int x;
public String toString(){
return String.valueOf(x);
}
}
}
必须知道,别滥用-finally
最新推荐文章于 2021-02-24 00:23:51 发布