About Question enthuware.ocajp.i.v8.2.1031 :
Moderators: Site Manager, fjwalraven
-
- Posts: 5
- Joined: Wed Jul 12, 2017 11:58 pm
- Contact:
About Question enthuware.ocajp.i.v8.2.1031 :
Why can't we throw null pointer exception from static block ? why does it give compile time error?
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1031 :
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.
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Wed Jul 12, 2017 11:58 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1031 :
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
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1031 :
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:
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.
Code: Select all
public class TestClass{
static{
if(System.getProperty("xyz") == null) throw new NullPointerException();
}
}
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.
-
- Posts: 5
- Joined: Wed Jul 12, 2017 11:58 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1031 :
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.
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1031 :
That is correct
If you like our products and services, please help us by posting your review here.
-
- Posts: 125
- Joined: Thu Jul 05, 2018 6:44 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1031 :
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.
In the answer it says "Please read ExceptionClassSummary document in the "Study References" section."
Where is this Study References section to be found? TYIA.
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1031 :
In the Study View tab of the tree on the left-side of ETS Viewer window.
If you like our products and services, please help us by posting your review here.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1031 :
There seems to be diferrent explanation in this forum then in the book:
The forum
The forum
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1031 :
I also have a question regarding Class.forName. It is not mentioned in the book do we need to know it for the exam?
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1031 :
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.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1031 :
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.
Who is online
Users browsing this forum: No registered users and 23 guests