我有这个代码:
public class C implements Closeable{
private Integer i = 0;
public C () {
}
public C (Integer i) {
this.i = i;
}
@Override
public void close() throws IOException {
System.out.println("close " + this.i);
}
public void m1(C other){
System.out.println("m1 " + this.i + " " + other.i);
}
public static void main(String[] args) {
C c1 = new C(1);
try (C c2 = new C(2); C c3 = null){
c1.m1(c2);
c2.m1(new C());
c3.m1(c1);
}
catch(IllegalArgumentException e) {
System.out.println("illegal argument exception");
}
catch(NullPointerException e) {
System.out.println("null pointer exception");
}
catch(Exception e) {
System.out.println("exception");
}
finally {
System.out.println("finally");
}
System.out.println("end");
如果我运行它,我将得到以下输出:
} m1 1 2 m1 2 0 close 2 空指针异常终于结束
我的问题是:是否有任何“算法”可以帮助我在运行前了解输出的外观?我认为在这种情况下,当我有可关闭(或可自动关闭的对象)时。
为了更清楚,我想到了一些事情: 1.当出现异常时,你有finally但没有catch,你的程序会找到第一个catch并调用它来解决这个异常。ETC
非常感谢!