0
package testdividebyzero;

import java.util.Scanner;

public class TestDivideByZero {

    public static void main(String[] args) {
  
            InputNumber();
    }
    
    static void InputNumber()
    {
    Scanner console=new Scanner (System.in);
            
        double num1=0,num2=0;
        
    System.out.print("Enter first number : ");
    num1=console.nextInt();
       
        System.out.print("Enter second number : ");
    num2=console.nextInt();
       
        double num3=Division(num1,num2);
        
        System.out.print("Result of the devision is:"+num3);
        
    }
    static double Division(double num1,double num2)
    {
        double num3 = 0;
        
       try{
            num3 = num1 / num2;
       }
        catch(ArithmeticException e)
        {
            System.out.println("ArithmeticException => " + e.getMessage());
        }
       
        return num3;
    }

}

This is my coding, when I try to divide with number zero it's not catch the exception

DEV
  • 1,607
  • 6
  • 19
Mizan
  • 1

2 Answers2

1

I would just do the following. You can either catch the exception yourself and proceed with some special handling or you can let the JVM report the exception.

public class DivideByZero {

    public static void main(String[] args) {
        try {
            System.out.println(compute(10, 0.));
        } catch (ArithmeticException ae) {
            System.out.println(ae.getLocalizedMessage() + ": Oops!");
        }
        System.out.println(compute(10, 0.));
    }

  
    public static double compute(double a, double b){
            if (b == 0) {
                throw new ArithmeticException("Division by zero!");
            }
            return a/b;
    }
    
}

prints

Division by zero!: Oops!
Exception in thread "main" java.lang.ArithmeticException: Division by zero!
    at stackOverflow.march._31_Mar_2023.DivideByZero.compute(DivideByZero.java:18)
    at stackOverflow.march._31_Mar_2023.DivideByZero.main(DivideByZero.java:11)
WJS
  • 36,363
  • 4
  • 24
  • 39
0

Jens and siarhei987 Have pretty much summed up the issue over here.

Ideally you should be accepting the double value from scanner as follows:

double num1 = scanner.nextDouble();

But again as per your question if you want a program that explicitly throw an ArithmeticException when divided by zero following code can be used:

static double Division(double num1,double num2)
    {
        double num3 = 0;
        
       try{
          num3 = num1 / num2;
          if (Double.isInfinite(num3)) {
           // we are throwing ArithmeticException on purpose to catch it in the Exception block
          throw new ArithmeticException("num3 is Infinite.");
          }
       }
        catch(ArithmeticException e)
        {
            System.out.println("ArithmeticException => " + e.getMessage());
        }
        return num3;
    }

Not a great way of doing things but again going by the requirements.

Hope this helps, Thanks.

Suchandra T
  • 569
  • 4
  • 8
  • 1
    are you sure that `num3` will be INFINITY only for division by 0? Wouldn't it be much easier to understand the intention and to implement it by just checking if `num2` is zero (before even dividing) - and not USING try-catch at all? - Exceptions should be an *exception*, not used for flow control – user16320675 Apr 02 '23 at 15:03
  • num3 can be infinite for many cases not just divided by zero case. and yes we can definitely check num2 for zero check and early exit the code which is good. but the author has an explicit requirement of throwing an ArithmeticException. thus the answer. – Suchandra T Apr 02 '23 at 15:10
  • Agreed by 1 & 2. currently, a few aspects are unclear. let's wait for the author to clarify the scenarios. – Suchandra T Apr 02 '23 at 15:40