Java Exception Handling (Try-catch)

  • + 0 comments

    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 scn = new Scanner(System.in);       
        try{
            int x = scn.nextInt();
            int y = scn.nextInt();
            if(y==0)
                {
                    System.out.println("java.lang.ArithmeticException: / by zero");
                    return;
                }
            System.out.print(x/y);
        }
        catch(Exception e)
        {
            System.out.println("java.util.InputMismatchException"); 
        }       
    
    }
    

    }