Java Exception Handling (Try-catch)

  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            Scanner sc = new Scanner(System.in);
            
            try{
                int x = sc.nextInt();
                 int y = sc.nextInt();
                if(x>y ){
                    System.out.println(x/y);
                }
            }
            catch(InputMismatchException e){
                 System.out.println("java.util.InputMismatchException");
            }
            catch(ArithmeticException e){
                System.out.println("java.lang.ArithmeticException: / by zero");
            }
            catch(Exception e){
                System.out.println(e);
            
            
            sc.close();
        }
        }
    }