Reputation: 614
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?
Upvotes: 1
Views: 363
Reputation: 140494
This:
public Foo foo = new Foo();
public Foo() throws Exception {
// Ctor body.
}
is rearranged by the compiler to be (try decompiling both forms of the class if you don't believe me!):
public Foo foo;
public Foo() throws Exception {
super();
foo = new Foo();
// Ctor body.
}
so it is executed somewhere where throws Exception
handles the exception.
This is described (although not explained) in JLS 11.2.3:
It is a compile-time error if an instance variable initializer (§8.3.2) or instance initializer (§8.6) of a named class can throw a checked exception class, unless the named class has at least one explicitly declared constructor and the exception class or one of its superclasses is explicitly declared in the throws clause of each constructor.
The explanation can be found in JLS 12.5, Creation of New Class Instances (bullets 3, 4 and 5 explain the rearrangement I mention above; emphasis added)
Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:
Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.
If this constructor begins with an explicit constructor invocation (§8.8.7.1) of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.
This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.
Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5.
Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.
Upvotes: 3