0

so I'm in a Java class right now and we're supposed to write a program using ArrayIndexOutOfBoundsExceptions. Simple enough, right? Well for whatever reason I'm getting the error:

"error: incompatible types: ArrayIndexOutOfBoundsException cannot be converted to Throwable"

import java.util.*; //for exceptions

class ArrayIndexOutOfBoundsException {
public static void main(String[]args){
int[] array = fillArray(); //filling the array with 100 values
int userInput = getInput();
try{
  System.out.print("The value of element "+userInput+" is "+array[userInput]);
}
catch (ArrayIndexOutOfBoundsException ex) {
  System.out.print("Out of Bounds");
}
}//end main

public static int[] fillArray(){ //function to fill the array
int[] array = new int[100];
for (int i = 0; i<100;i++)
{
  array[i] = (int)(Math.random() *100) + 1;
}
return array;
}//end fillArray

public static int getInput(){ //function to make sure the user enters a proper value
System.out.print("Please enter the index value to find the corresponding value at that location: ");
Scanner input = new Scanner(System.in); //for input
int userInput = 0;
try{
  userInput = input.nextInt();
}
catch(InputMismatchException ex){
  System.out.println("\nNot a valid number\nPlease Try Again"); 
  return getInput();
}
return userInput;
} //end getInput

}//end class

This is the code I'm running, with the error occurring at the "catch (ArrayIndexOutOfBoundsException ex) {".

However, if I switch that to just an IndexOutOfBoundsException the code works as intended and I cannot for the life of my figure out why. I mean, it's being applied to an Array so shouldn't an ArrayIndexOutOfBoundsException apply? At least that's what I've been able to find trying to google the problem...but clearly there is something else I'm missing.

I'm running it on Repl.it, I don't think that would make a difference, but never know?

Also, I apologize for the poorly formatted code, for whatever reason when I copy and paste it, it get's all messed up so this was the best I was able to fix it to make it readable in a reasonable amount of time.

Anyways, any help/advice/so much as pointing me in the right direction would be appreciated! Thank you!

2 Answers2

1

Change the name of your class and it will work. This is the best solution, as it stops the masking of java.lang.ArrayIndexOutOfBoundsException:

class ArrayIndexOutOfBoundsExceptionTest {
  public static void main(String[]args){
    int[] array = fillArray(); //filling the array with 100 values
    int userInput = getInput();
    try{
      System.out.print("The value of element "+userInput+" is "+array[userInput]);
    }
    catch (ArrayIndexOutOfBoundsException ex) {
      System.out.print("Out of Bounds");
    }
  }//end main

For completeness and demonstration purposes, here I haven't renamed the class, but instead used the full pathname (java.lang) for the Exception

class ArrayIndexOutOfBoundsException { 
  public static void main(String[]args){
    int[] array = fillArray(); //filling the array with 100 values
    int userInput = getInput();
    try{
      System.out.print("The value of element "+userInput+" is "+array[userInput]);
    }
    catch (java.lang.ArrayIndexOutOfBoundsException ex) {
      System.out.print("Out of Bounds");
    }
  }//end main
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
-1

There are a couple of problems with your implementation.

  1. There is already a class called ArrayIndexOutOfBoundsException.
  2. You will see in the documentation that the class's hierarchy includes Throwable, which your class does not.

Screenshot

So you need your class to either extend the IndexOutOfBoundsException or the Throwable class.

tomerpacific
  • 4,704
  • 13
  • 34
  • 52