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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Ardaks
Posts: 1
Joined: Tue Jun 30, 2020 1:40 pm
Contact:

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

Post by Ardaks »

Hi admin. Could you please explain, why ExceptionInInitializerError is not thrown in the forth point?

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

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

Post by admin »

ExceptionInInitializerError can be thrown when the code is run. But before you can run the code, it has to be compiled. Option 4 fails during compilation, which means, you can't run it. So, an ExceptionInInitializerError will not be thrown.

arty9608
Posts: 1
Joined: Thu Jul 10, 2025 6:20 pm
Contact:

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

Post by arty9608 »

Hi, just incase there's anyone wondering:
Compiling the fourth points results in the following compile time error:

------------------------------
X.java:16: error: initializer must be able to complete normally
static {
^
1 error
------------------------------

Java essentially sees that the static block will never complete and flags the error.

If we modify the code a little and trick the compiler into thinking that the block might execute normally, in that case we can get the ExceptionInInitializerError:

-------------------------------
public class X {
static {
if (true) // Compiler thinks this may or may not be true.
throw new NullPointerException(); // This is okay!
}

public static void main(String[] args) {

}
}
-------------------------------

Post Reply

Who is online

Users browsing this forum: No registered users and 162 guests