0

I want to understand exception propagation in java at thread level.

My understanding is that when ever a code throws an exception and if the method is not handling that exception then it is stored in the stack of that thread and jvm just keeps popping out method calls from stack if exception is not handled. This method will apply to both Checked and unchecked exceptions. Please correct me if i am wrong.

With above explanation i am not able to understand how are exceptions in static block handled because they will be called during class loading time.

Any ideas on this?

Lokesh
  • 7,810
  • 6
  • 48
  • 78

2 Answers2

1

The code inside static block is also executed in a thread (even if it is indeed called at class initialization time) and so the same strategy applies to unchecked exceptions thrown from a static block.

Note that you will get a compilation error if your code throws a checked exception from a static block.

ben75
  • 29,217
  • 10
  • 88
  • 134
1

Java strictly and precisely defines the moment at which a class is initialized (as opposed to loaded, BTW!). It is always triggered by some Java code, so the particular line of Java code that triggers the class init will receive the exception. There is no magic, no special cases, and it always happens at runtime, just like any other exceptions.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436