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

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
vidyats
Posts: 5
Joined: Wed Jul 12, 2017 11:58 pm
Contact:

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

Post by vidyats »

Why can't we throw null pointer exception from static block ? why does it give compile time error?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

vidyats
Posts: 5
Joined: Wed Jul 12, 2017 11:58 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

vidyats
Posts: 5
Joined: Wed Jul 12, 2017 11:58 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

That is correct :)
If you like our products and services, please help us by posting your review here.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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 7407 times
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 »

There seems to be diferrent explanation in this forum then in the book:
Image

The forum
Image

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests