Possible Duplicate:
Exception in static initialization block
It looks like static initializer cannot throw exceptions, I would like to know the reason b开发者_如何学Pythonehind it.
Static initializers can't throw checked exceptions, because initialization of a class can happen at effectively arbitrary places in the code at which point the checked exception would not be expected.
Static initializers can throw unchecked exceptions, which will prevent the class from being initialized correctly and will prevent the class from being used. Doing that will lead to terribly hard to debug problems, however.
The rule for this is in §8.7 Static Initializers of the JLS:
It is a compile-time error for a static initializer to be able to complete abruptly (§14.1, §15.6) with a checked exception (§11.2).
精彩评论