Write a program – “DivideByZero” that takes two numbers a and b as input, computes a/b, and invokes Arithmetic Exception to generate a message when the denominator is zero

Write a program – “DivideByZero” that takes two numbers a and b as input, computes a/b, and invokes Arithmetic Exception to generate a message when the denominator is zero.

Program-01: Without Scanner

public class DivideByZero

{

    public static void main (String[] args)

    {

        int a=34;

        int b=0;

        int c;

        try

        {

             c=a/b;

             System.out.println("Value of"+a+"/"+b+"="+c);

        }

        catch (ArithmeticException e)

                {

                System.out.println(e);

                }

    }

}


OutPut:

java.lang.ArithmeticException: / by zero


Program-02: With Scanner 

import java.util.Scanner;

public class ArithException 

{

    public static void main(String[] args) 

    {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter Value of a : ");

        int a = sc.nextInt();

        System.out.print("Enter Value of b : ");

        int b = sc.nextInt();

        int c;

        sc.close();

        try {

            c = a / b;

            System.out.print("Value of " + a + " / " + b + " = " + c);

        } catch (ArithmeticException e) {

            System.out.println(e);

        }

                

    }

}   


Output:

Enter Value of a : 55

Enter Value of b : 0

java.lang.ArithmeticException: / by zero