Kushal Cse3c 184
Kushal Cse3c 184
output:
Q2:
Write a program in JAVA to read an integer array of size 10 and also
display the element at the 12th index position of the array. Handle the
exception in such a way that your program will not abort.
Code:
import java.util.*;
public class pos{
public static void main(String[] args) {
Scanner ob = new Scanner(System.in);
int[] arr = new int[10];
System.out.println("Enter 10 integers:");
for (int i = 0; i <10; i++) {
arr[i] = ob.nextInt();
}
try {
System.out.println("Element at index 12: " + arr[12]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error Message: Index 12 is out of bounds for an array with
size=10");
}
}
}
Output: