If your static initializer throws any exception, the thrown exception is wrapped in the java.lang.ExceptionInInitializerError and then thrown.
java.lang.ExceptionInInitializerError Caused by: java.lang.ArithmeticException: / by zero at generalissues.InitBlockTest.<clinit>(InitBlockTest.java:6) Exception in thread "main"
Joined: Feb 23, 2007
Thanks Barry and chandra
one more thing
In static initiliazer we can't throw Checked Exceptions.
am i right?
Thanks Anil Kumar
Joined: Feb 28, 2007
In static initiliazer we can't throw Checked Exceptions. am i right?
Static initializer must complete normally. As Barry gave an example that if instance initializer throws a checked exception the constructor must declare that exception to be caught. In case of static initializer, you can place the code that may throw exception in the try catch block. Like:
Instance init block also can't throw checked exception until you handle it or declare it in the constructor.