Import Java - io.IOException
Import Java - io.IOException
IOException ;
import java.io.InputStreamReader ;
import java.io.BufferedReader ;
public class ExceptionalHandling{
public static void main(String[] args){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
try {
System.out.println("Choose the Error u want to Display");
System.out.println("1.Arithmetic Exception ");
System.out.println("2.Array out of Bound Exception ");
System.out.println("3.Null Pointer Exception ");
System.out.println("4.Number Format Exception");
System.out.println("5.IO Exception ");
int choice = Integer.parseInt(br.readLine());
switch (choice) {
case 1 :
getArithmeticException(br);
break ;
case 2 :
getArrayOutOfBound(br) ;
break ;
case 3 :
getNullPointer(br) ;
break ;
case 4 :
getNumberFormat(br);
break ;
case 5 :
getIOException();
break ;
case 6 :
br.close();
break ;
default :
System.out.println("Invalid Choice......");
}
}
catch(IOException e){
System.out.println("Caught" + e);
}
}
}
public static void getArithmeticException(BufferedReader br) throws
IOException{
try{
System.out.print("Enter the Numerator ");
int num ;
num = Integer.parseInt(br.readLine());
System.out.print("Enter the Number : ");
int divisor = Integer.parseInt(br.readLine());
int divide = num / divisor ;
System.out.println("Result :- " + divide );
}catch(ArithmeticException e){
System.out.println("Caught(Divide by zero)" + e);
}
}
public static void getArrayOutOfBound(BufferedReader br) throws IOException {
try{
int [] arr = new int[5] ;
System.out.println("Enter Index : ");
int index = Integer.parseInt(br.readLine());
arr[index] = 10 ;
System.out.println("Value of Index is :" + index);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Caught" + e);
}
}