When some method or constructor declares exception, the caller must be handle it. It of course works even in initialization blocks. For example:
class Foo {
private static Foo foo = new Foo();
public Foo() throws Exception {
}
}
Compile error on line 2, which can be fixed like this:
class Foo {
static Foo foo;
static {
try {
foo = new Foo();
}
catch (Exception e) {
e.printStackTrace();
}
}
public Foo() throws Exception {
}
}
But when we remove the static modifier, the compiler no longer swears:
class Foo {
public Foo foo = new Foo();
public Foo() throws Exception {
}
}
I know, that this code is always will be throws StackOverflowException due the infinite objects creation, but why there are no compiler error about declared exception?