Exceptions
Thu Dec 21 2023 23:38:38 GMT+0000 (Coordinated Universal Time)
Saved by
@user01
import java.util.*;
public class ExcepDemo{
public static void main(String[]args){
demonstrateExceptions();
}
public static void demonstrateExceptions(){
Scanner sc = new Scanner(System.in);
try{
System.out.println("numerator");
int n1 = sc.nextInt();
System.out.println("Denominator");
int n2 = sc.nextInt();
int result = n1/n2;
System.out.println("Result"+result);
}
catch(ArithmeticException e){
System.out.println("Arithmetic exception occured"+e.getMessage());
}
catch(ArrayIndexOutOfBoundsException e1){
System.out.println("AIOOfBE"+e1.getMessage());
}
catch(NullPointerException e2){
System.out.println("Null pointer exception occured"+e2.getMessage());
}
catch(Exception e3){
System.out.println("Caught generic exception"+e3.getMessage());
}
finally{
System.out.println("Always executed");
}
}
}
content_copyCOPY
Comments