Skip to content

Instantly share code, notes, and snippets.

@nobeans
Created April 20, 2011 06:30
Show Gist options
  • Save nobeans/930499 to your computer and use it in GitHub Desktop.
Save nobeans/930499 to your computer and use it in GitHub Desktop.
import java.io.IOException;
public class MultiCatch {
static class X extends Exception {
public void x() { System.out.println("XXX!"); }
};
static class A extends X {
public void a() { System.out.println("AAA!"); }
};
static class B extends X {
public void b() { System.out.println("BBB!"); }
};
static class A2 extends A {
public void a2() { System.out.println("AAA222!"); }
};
public static void main(String... args) {
try {
if (args.length == 1) {
throw new A();
}
if (args.length == 2) {
throw new B();
}
} catch (A|B e) {
System.out.println("ex: " + e.getClass());
//e.a(); // MultiCatch.java:28: シンボルを見つけられません
//e.b(); // MultiCatch.java:28: シンボルを見つけられません
e.x(); // AとBの共通なのでOK
}
try {
if (args.length == 0) {
throw new A2();
}
if (args.length == 1) {
throw new A();
}
if (args.length == 2) {
throw new B();
}
} catch (A2|A|B e) {
System.out.println("ex: " + e.getClass());
//e.a(); // MultiCatch.java:28: シンボルを見つけられません
//e.b(); // MultiCatch.java:28: シンボルを見つけられません
//e.a2(); // MultiCatch.java:28: シンボルを見つけられません
e.x(); // AとBとA2の共通なのでOK
}
try {
if (args.length == 0) {
throw new A2();
}
if (args.length == 1) {
throw new A();
}
} catch (A2|A e) {
System.out.println("ex: " + e.getClass());
e.a(); // AとA2の共通なのでOK
//e.b(); // MultiCatch.java:28: シンボルを見つけられません
//e.a2(); // MultiCatch.java:28: シンボルを見つけられません
e.x(); // AとA2の共通なのでOK
}
System.out.println("Done.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment