MIT-Curricular/OOP/Java/Lab/Week10/Array_Exception.java

40 lines
931 B
Java
Raw Permalink Normal View History

2024-10-14 02:40:35 +05:30
import java.util.*;
class ArrayOverflowException extends Exception
{
int a;
ArrayOverflowException(int b)
{
a=b;
}
public String toString()
{
return "ArrayOverflowException:"+a+" is out of bound";
}
}
public class Array_Exception {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int arr[]=new int[10];
for(int k=0;k<arr.length;k++)
arr[k]=k;
System.out.println("Enter the index:");
int i;
try{
i=in.nextInt();
if(i<arr.length)
{
System.out.println("Value="+arr[i]);
}
else
{
throw new ArrayOverflowException(i);
}
}
catch(ArrayOverflowException e)
{
System.out.println(e.toString());
}
}
}