Upload files to "OOP/Java/Lab/Week10"
This commit is contained in:
parent
2b9a21ac62
commit
22f306573d
5 changed files with 160 additions and 0 deletions
39
OOP/Java/Lab/Week10/Array_Exception.java
Normal file
39
OOP/Java/Lab/Week10/Array_Exception.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue