| Author |
ExceptionInInitializerError Vs ArrayIndexOutOfBoundsException
|
Harikrishna Gorrepati
Ranch Hand
Joined: Sep 23, 2010
Posts: 422
|
|
Hi, Please advice how ExceptionInInitializerError differs from ArrayIndexOutOfBoundsException.
I have given my understanding as given below in the comments.
|
OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
|
 |
Prasad Kharkar
Ranch Hand
Joined: Mar 07, 2010
Posts: 438
|
|
you must be knowing that static fields run according to the top down order here we are creating static array variable and then static initializer blockin the static array creation, the array is created firstnow in the static init block you are referring to the array index 5Always remember that the arrays in java are zero index based so the declaration new int[4] will create array containing 4 values that will be indexed as 0,1,2,3.Now, by writing x[4] you are actually referring to the FIFTH element of array which is not possible at all as we created array of just 4 elementsso this causes the ArrayIndexOutOfBoundsExceptionand you are trying to give the value 5 to a variable that does not exist and cannot exist at all due to wrong array indexThat means you cannot initilize the x[4] with value 5 and hence you are getting the ExceptionInInitializerError
Hope this helps
have a nice time
|
SCJP 6 [86%] June 30th, 2010
If you find any post useful, click the "plus one" sign on the right
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
ExceptionInInitializerError : Signals that an unexpected exception has occurred in a static initializer. An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable.
So, it will be thrown if there is a exception thrown in a static initializer. Have a look on the below example,
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
 |
|
|
subject: ExceptionInInitializerError Vs ArrayIndexOutOfBoundsException
|
|
|