Here is some code that works for me:
import java.util.Scanner;
public class badsubscriptcaught {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] names = {"Jake", "Jack", "Jacob", "Ben", "Liam", "Noah", "Oliver", "William", "Elijah", "James"};
int number = Integer.parseInt(scanner.nextLine());
try{
System.out.println(names[number]);
}catch ( exception){
System.out.println(exception + " isn't a vailid value.");
}
}
}