We have a array of element 5 which is (1, 2, 3, 4, 5) but when a user enters a index of an array which is Out of Bound means the entered index is not present in the array then the intr() function will throw the ArrayIndexOutOfBound Exception.
Program -
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main(){
int myArray[5] = {1,2,3,4,5};
int a;
int element;
int i;
int length = sizeof(myArray)/sizeof(myArray[5]);
printf("Elements in the array are: \n");
for(int i = 0; i < length; i++){
printf("%d",myArray[i]);
}
printf("\nEnter the index of the required elements: \n");
scanf("%d", &a);
if(a>=6)
intr();
else{
printf("Element in the given index is: ");
scanf("%d", &a);
}
getch();
}
intr()
{
printf("serving interrupt\n");
printf("Array Index Out Of Bound");
}
Output -
Elements in the array are:
12345
Enter the index of the required elements:
7
serving interrupt
Array Index Out Of Bound
0 Comments