Upload files to "OOP/Java/Lab/Week10"

This commit is contained in:
Aadit Agrawal 2024-10-14 02:40:35 +05:30
parent 2b9a21ac62
commit 22f306573d
5 changed files with 160 additions and 0 deletions

View file

@ -0,0 +1,39 @@
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());
}
}
}