aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ExceptionInInitializerError Vs ArrayIndexOutOfBoundsException Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "ExceptionInInitializerError Vs ArrayIndexOutOfBoundsException" Watch "ExceptionInInitializerError Vs ArrayIndexOutOfBoundsException" New topic
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 block
  • in the static array creation, the array is created first
  • now in the static init block you are referring to the array index 5
  • Always 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 elements
  • so this causes the ArrayIndexOutOfBoundsException
  • and you are trying to give the value 5 to a variable that does not exist and cannot exist at all due to wrong array index
  • That 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%|
     
    I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
     
    subject: ExceptionInInitializerError Vs ArrayIndexOutOfBoundsException
     
    Similar Threads
    unboxing issues
    Array Bounds
    ArrayIndexOutOfBoundsException?
    arrays and for each
    Array index