views:

767

answers:

4

Why doesn't Java allow to throw an exception from static initialization block? What was the reason behind this design decision?

Any help would be greatly appreciated.

Thanks.

+1  A: 

Take a look at the Java Language Specifications: it is stated that it is a compile time error if static initializer fails is able to complete abruptly with a checked exception.

Laurent Etiemble
This doesn't answer the question though. he asked *why* it is a compile time error.
Winston Smith
Hmm, so throwing any RuntimeError should be possible, because JLS only mentions checked exceptions.
Andreas_D
That's right, but you'll never see as a stacktrace. That's why you need to be careful with static initialization blocks.
EJB
+9  A: 

Because it is not possible to handle these exceptions in your source. You do not have any control over the initialization process and static{} blocks cannot be called from your source so that you could surround them with try-catch.

Because you cannot handle any error, it was decided to disallow exception-throwing in static blocks.

Update: Thanks commenters for the correction. The static block must not throw checked exceptions but still allows unchecked/runtime-exceptions to be thrown. But according to above reasons you would be unable to handle these either.

To summarize, this restriction prevents (or at least makes it harder for) the developer from building something which can result in errors from which the application would be unable to recover.

Kosi2801
Actually, this answer is inaccurate. You CAN throw exceptions in a static block. What you cannot do is to allow a *checked* exception to *propagate out of* a static block.
Stephen C
+3  A: 

It would have to look like this (this is not valid Java code)

static throws SomeCheckedException {
  throw new SomeCheckedException();
}

but how would ad where you catch it? Checked exceptions require catching. Imagine some examples that may initialize the class (or may not because it is already initialized), and just to draw the attention of the complexity of that it would introduce, I put the examples in another static initalizer:

static {
  try {
     ClassA a = new ClassA();
     Class<ClassB> clazz = Class.forName(ClassB.class);
     String something = ClassC.SOME_STATIC_FIELD;
  } catch (Exception oops) {
     // anybody knows which type might occur?
  }
}

And another nasty thing"

interface MyInterface {
  final static ClassA a = new ClassA();
}

Imagine ClassA had a static initializer throwing a checked exception: In this case MyInterface (which is an interface with a 'hidden' static initializer) would have to throw the exception or handle it - exception handling at an interface? Better leave it as it is...

Andreas_D
+2  A: 

Since no code you write can call static initialization block, it is not useful to throw checked exceptions. If it were possible, what would the jvm do when a checked exceptions are thrown? Runtimeexceptions are propagated up.

fastcodejava
Well, yes I understand the thing now. It was very silly of me to post a question like this. But alas... I can't delete it now. :( Nevertheless, +1 for your response...
missingfaktor
Rahul - It is not a silly question.
fastcodejava