开发者

why the static initializer cannot throw expetion [duplicate]

开发者 https://www.devze.com 2023-02-24 07:40 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Exception in static initialization block
This question already has answers here: Closed 11 years ago.

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消