Java Exception Handling (Try-catch)

  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            String x1 = scan.nextLine();
            String y1 = scan.nextLine();
            try{
                int x = Integer.parseInt(x1);
                int y = Integer.parseInt(y1);
                if(x==0||y==0){
                    System.out.println("java.lang.ArithmeticException: / by zero");
                }
                else{
                    System.out.println(x/y);
                }
            }
            catch(Exception ex){
                System.out.println("java.util.InputMismatchException");
            }
        }
    }