-2

I would like to know how to add an if statement for the following error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at Calendar.main(Calendar.java:14)

In other words, I would like to display the following text when the following error is triggered, "Error. Please enter the missing elements."

Anonymous
  • 1
  • 1
  • `if(idx >= myarray.length){ ... }` – Derlin Mar 29 '18 at 13:36
  • It's hard to be able to advise on the best approach without knowing what the code looks like. We don't know whether the index is dynamic and the array fixed, or vice versa. But yes, basically you'd check whether the array is as large as you expect it to be before indexing into it. – Jon Skeet Mar 29 '18 at 13:37
  • Maroun that article does not suggest a way to add a condition if the error occurs. I want to do something if the error occurs. – Anonymous Mar 29 '18 at 13:40

1 Answers1

0

Use the try catch statement

try{
    //do something that causes that error
catch(ArrayIndexOutOfBoundsException e){
   //error ocurred
}
Javier S
  • 379
  • 3
  • 13