About Question enthuware.ocajp.i.v8.2.1031 :
Moderator: admin
- 
				Ardaks
 - Posts: 1
 - Joined: Tue Jun 30, 2020 1:40 pm
 - Contact:
 
About Question enthuware.ocajp.i.v8.2.1031 :
Hi admin. Could you please explain, why ExceptionInInitializerError is not thrown in the forth point?
			
			
									
									
						- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v8.2.1031 :
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 :
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) {
        
}
}
-------------------------------
			
			
									
									
						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) {
}
}
-------------------------------
Who is online
Users browsing this forum: No registered users and 9 guests