Java Exception constructors

I am attempting to get more detail about a particular cause of an exception surfaced to my application.

I am catching an ArrayIndexOutOfBoundsException.
That inherits RuntimeException which has a constructor accepting both a String and a Throwable cause.,%20java.lang.Throwable)

So I attempt to catch the exception and add detail as I re-throw the exception:
catch(ArrayIndexOutOfBoundsException ex){
           throw new ArrayIndexOutOfBoundsException(String.format("Input was %s", Arrays.toString(value)),ex); 

Open in new window

The String.format is working as expected.  Without the final ",ex", the code works OK, but I lose the information in the original Exception.  The code above does not compile.
no suitable constructor found for ArrayIndexOutOfBoundsException(java.lang.String,java.lang.ArrayIndexOutOfBoundsException)

Where is my mistake?

I'm using Java 7.

LVL 32
Daniel WilsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

In this case ArrayIndexOutOfBoundsException itself only has 3 constructors and none of them take an exception as the second argument:

    public ArrayIndexOutOfBoundsException() {
    public ArrayIndexOutOfBoundsException(int index) {
        super("Array index out of range: " + index);
    public ArrayIndexOutOfBoundsException(String s) {

Open in new window

It doesn't inherit constructors from its base class.  They need to be defined in this class.

(You might ask why this class doesn't accept an exception as a second argument - I'd suspect the reason is that the second argument exception is meant to be an underlying cause, and ArrayIndexOutOfBoundsException isn't the result of something else happening, it's a primary cause of an exception.  In any case, it's a language choice - not something we can change).

You might want to try something like this instead:

		catch(ArrayIndexOutOfBoundsException ex){
			throw new IllegalArgumentException(String.format("Input was %s", Arrays.toString(value)),ex);

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark OlsenSr. DeveloperCommented:
Another option would be to call "initCause" after creating the exception and before throwing it. The cause could then be accessed when the exception is handled.

catch(ArrayIndexOutOfBoundsException ex){
   ArrayIndexOutOfBoundsException  arrayIndexOutOfBoundsException = 
      new ArrayIndexOutOfBoundsException(String.format("Input was %s", Arrays.toString(value)));
   throw arrayIndexOutOfBoundsException;

Open in new window

Daniel WilsonAuthor Commented:
Thank you both!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.