0% found this document useful (0 votes)
33 views

Import Java - io.IOException

Uploaded by

riddhichaskar750
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Import Java - io.IOException

Uploaded by

riddhichaskar750
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.io.

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);
}
}

public static void getNullPointer(BufferedReader br)throws IOException{


try{
System.out.println("Enter the String : ");
String str = br.readLine();
if (str.isEmpty()){
str = null ;
}
System.out.println("String Length :" + str.length());
}catch(NullPointerException e){
System.out.println("Caught" + e);
}
}

public static void getNumberFormat(BufferedReader br) throws IOException{


try{
System.out.println("Enter the Non Numeric String : ");
String str = br.readLine();
int num = Integer.parseInt(str);
System.out.println("Parsed Number : " + num);
}catch(NumberFormatException e){
System.out.println("Caught" + e);
}
}

public static void getIOException(){


try{
throw new IOException("Triggered");
}
catch(IOException e){
System.out.println("Caught" + e);
}
System.out.println();
}
}

You might also like