0

I have this code:

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");

If I run it, I will get following output:

} m1 1 2 m1 2 0 close 2 null pointer exception finally end

My question is: Is there any "algorithm" which can help me to know how output will look before run? I think in this case when I have closeable (or autocloseable object).

To be more clear, I think something about: 1. When exception appear, and you have finally but not catch, your program will find first catch and call it to solve this exception. etc

Many thanks!

o.hujs
  • 1
  • Not sure what you are asking here? Note that if you use try-with-resources, several exceptions can be collected -- the first one to be thrown wins, the others are "suppressed" (see Throwable#getSuppressed). – fge Sep 13 '16 at 10:41
  • @Marko Topolnik how can I format code? – o.hujs Sep 13 '16 at 10:45

0 Answers0