AFAIK it is possible to throw an exception in an initialization block or in an instance variable initialization expression as far as all of its class' constructors declare this exception provided at least one constructor is available. However an anonymous class can throw any exception in its initialization blocks/expressions.
Somehow, it doesn't want to work for me :/
This one however compiles:
Could anybody please explain it to me ? :/
What about not checked exceptions ?
Also, I would expect that it doesn't apply to static initialization block, but since the above doesn't work for me, I can't check this one.
The reason for your problem: The compiler checks if it is - at least theoretically - possible that the commands within an INSTANCE initializer block are able to complete normally.
Without your if-condition this it not possible, since the exception will be ALWAYS thrown, and therefore
fails to compile. Note: it does neither matter whether this is an initializer block of a named or anonymous class nor if you throw a checked or unchecked exception.
The same is true for STATIC initializer blocks: The compiler checks if it is - at least theoretically - possible that the commands within an static initializer block are able to complete normally. Thus, will not compile.