ArrayIndexOutOfBounds exception using try-catch block in java

Write a program to handle the ArrayIndexOutOfBounds exception using try-catch block in java.

package Exception_demo;
import java.util.*;
public class arrayoutofbound_soln 
{
    public static void main(String args[])
    {
        Scanner s=new Scanner(System.in);
        int size;
        System.out.println("Enter size of array");
        size=s.nextInt();
        int a[]=new int[size];
        int i;
        System.out.println("enter the elements::");
        try
        {
            for(i=0;i<10;i++)
            {
                a[i]=s.nextInt();            
            }
            for(i=0;i<size;i++)
            {
                System.out.println("elements are::"+a[i]);
            }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *