Page 1 of 1

About Question enthuware.ocajp.i.v8.2.1031 :

Posted: Thu Jul 13, 2017 12:36 am
by vidyats
Why can't we throw null pointer exception from static block ? why does it give compile time error?

Re: About Question enthuware.ocajp.i.v8.2.1031 :

Posted: Thu Jul 13, 2017 1:45 am
by admin
The error message given by the compiler explains the reason, " initializer must be able to complete normally". When the JVM tries load a class, it will execute the static initializer. If the initializer is written such a way that it will always throw an exception, it can never load the class. Then what is the point of having that class? So the compiler refuses to compile it.

Paul.

Re: About Question enthuware.ocajp.i.v8.2.1031 :

Posted: Thu Jul 13, 2017 1:54 am
by vidyats
Sorry I am still not clear. Do u mean static block cannot throw any exception ?But I read it can throw run time exception and which in turn leads to ExceptionInInitializerError

Re: About Question enthuware.ocajp.i.v8.2.1031 :

Posted: Thu Jul 13, 2017 1:58 am
by admin
Yes, it can throw any run time exception but the issue is whether it will throw an exception always or only in some cases. For example, the following will compile fine:

Code: Select all

public class TestClass{
 static{
   if(System.getProperty("xyz") == null) throw new NullPointerException();
 }
}
Here the compiler cannot say for sure that the static block will always throw an exception. So it has no problem in compiling the code.

The rule is different for checked exceptions. Compiler does not like even the possibility of a checked exception from a static initializer.

Re: About Question enthuware.ocajp.i.v8.2.1031 :

Posted: Thu Jul 13, 2017 2:08 am
by vidyats
Thanks . So in obvious situation of static block throwing an error compiler will warn us with compile time error otherwise it will let run the code and if code throws any exception at run time then ExceptionInInitializerError is thrown and output the same on console.

Re: About Question enthuware.ocajp.i.v8.2.1031 :

Posted: Thu Jul 13, 2017 2:15 am
by admin
That is correct :)

Re: About Question enthuware.ocajp.i.v8.2.1031 :

Posted: Fri Jul 06, 2018 11:33 am
by __JJ__
Hello
In the answer it says "Please read ExceptionClassSummary document in the "Study References" section."
Where is this Study References section to be found? TYIA.

Re: About Question enthuware.ocajp.i.v8.2.1031 :

Posted: Fri Jul 06, 2018 11:44 am
by admin
In the Study View tab of the tree on the left-side of ETS Viewer window.
test.png
test.png (31.2 KiB) Viewed 9687 times

Re: About Question enthuware.ocajp.i.v8.2.1031 :

Posted: Fri Feb 08, 2019 5:05 pm
by flex567
There seems to be diferrent explanation in this forum then in the book:
Image

The forum
Image

Re: About Question enthuware.ocajp.i.v8.2.1031 :

Posted: Fri Feb 08, 2019 5:07 pm
by flex567
I also have a question regarding Class.forName. It is not mentioned in the book do we need to know it for the exam?

Re: About Question enthuware.ocajp.i.v8.2.1031 :

Posted: Sat Feb 09, 2019 2:43 am
by admin
flex567 wrote:
Fri Feb 08, 2019 5:05 pm
There seems to be diferrent explanation in this forum then in the book:
The book is correct. The explanation that I wrote applies to only "unchecked" exception. So, I should have written "any unchecked exception" in my post above. (Updating it now).
The book is talking above checked exception and it is true that you cannot even have the possibility of a checked exception from the static initializer.

thank you for your feedback!
Paul.

Re: About Question enthuware.ocajp.i.v8.2.1031 :

Posted: Sun Feb 10, 2019 7:11 am
by flex567
What about this?
flex567 wrote:
Fri Feb 08, 2019 5:07 pm
I also have a question regarding Class.forName. It is not mentioned in the book do we need to know it for the exam?

Re: About Question enthuware.ocajp.i.v8.2.1031 :

Posted: Sun Feb 10, 2019 9:20 pm
by admin
Class.forName is border line. It is not on the exam but some people have seen this mentioned in the options. It is good to know anyway.