2007-07-15 19:03:02 -06:00
|
|
|
public class Exceptions {
|
2014-07-10 13:27:18 -06: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-29 20:39:01 -06:00
|
|
|
|
|
|
|
private static void evenMoreDangerous() {
|
|
|
|
throw new RuntimeException("chaos! panic! overwhelming anxiety!");
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void moreDangerous() {
|
2007-07-01 19:42:35 -06:00
|
|
|
evenMoreDangerous();
|
2007-06-29 20:39:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void dangerous() {
|
|
|
|
moreDangerous();
|
|
|
|
}
|
|
|
|
|
2014-07-10 13:27:18 -06:00
|
|
|
private static void expect(boolean v) {
|
|
|
|
if (! v) throw new RuntimeException();
|
|
|
|
}
|
|
|
|
|
2007-06-29 20:39:01 -06:00
|
|
|
public static void main(String[] args) {
|
2014-07-10 13:27:18 -06:00
|
|
|
boolean threw = false;
|
2007-07-10 22:19:26 -06:00
|
|
|
try {
|
|
|
|
dangerous();
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
2014-07-10 13:27:18 -06: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-10 22:19:26 -06:00
|
|
|
}
|
2014-07-10 13:27:18 -06:00
|
|
|
expect(threw);
|
2007-06-29 20:39:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|