/* related posts with thumb nails */

ArrayIndexOutOfBoundsException:

The following program demonstrates catching ArrayIndexOutOfBoundsException. It is raised in a program when we use invalid index i.e. an index value i.e. greater than or equal to its size is used.

class Test
{
   public static void main(String as[])
   {
      int a[]={10,20,30,40};

      try
      {
         for(int i=0;i<10;i++)
          System.out.print("\t"+a[i]);
      }
      catch(ArrayIndexOutOfBoundsException ae)
      {
         System.out.println("Array has "+a.length+" elements only");
      }
   }
}

The above program produces the following output.
   10      20      30      40
   Array has 4 elements only
Related Topics:

0 comments:

Post a Comment