2007-07-16 01:03:02 +00:00
|
|
|
public class Exceptions {
|
2014-07-10 19:27:18 +00:00
|
|
|
static class ThrowError {
|
|
|
|
static {
|
|
|
|
if (true) throw new AssertionError();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void foo() { }
|
|
|
|
}
|
|
|
|
|
|
|
|
static class ThrowException {
|
|
|
|
static {
|
|
|
|
if (true) throw new RuntimeException();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void foo() { }
|
|
|
|
}
|
2007-06-30 02:39:01 +00:00
|
|
|
|
|
|
|
private static void evenMoreDangerous() {
|
|
|
|
throw new RuntimeException("chaos! panic! overwhelming anxiety!");
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void moreDangerous() {
|
2007-07-02 01:42:35 +00:00
|
|
|
evenMoreDangerous();
|
2007-06-30 02:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void dangerous() {
|
|
|
|
moreDangerous();
|
|
|
|
}
|
|
|
|
|
2014-07-10 19:27:18 +00:00
|
|
|
private static void expect(boolean v) {
|
|
|
|
if (! v) throw new RuntimeException();
|
|
|
|
}
|
|
|
|
|
2007-06-30 02:39:01 +00:00
|
|
|
public static void main(String[] args) {
|
2014-07-10 19:27:18 +00:00
|
|
|
boolean threw = false;
|
2007-07-11 04:19:26 +00:00
|
|
|
try {
|
|
|
|
dangerous();
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
2014-07-10 19:27:18 +00:00
|
|
|
threw = true;
|
|
|
|
}
|
|
|
|
expect(threw);
|
|
|
|
threw = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
ThrowError.foo();
|
|
|
|
} catch (AssertionError e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
threw = true;
|
|
|
|
}
|
|
|
|
expect(threw);
|
|
|
|
threw = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
ThrowException.foo();
|
|
|
|
} catch (ExceptionInInitializerError e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
threw = true;
|
2007-07-11 04:19:26 +00:00
|
|
|
}
|
2014-07-10 19:27:18 +00:00
|
|
|
expect(threw);
|
2007-06-30 02:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|