How to capture divide by zero exception in Java?

Java 8Object Oriented ProgrammingProgramming

When you divide a number by zero an Arithmetic Exception number is thrown.

Example

Live Demo

public class DividedByZero {
   public static void main(String args[]) {
      int a, b;
      try {
         a = 0;
         b = 54/a;
         System.out.println("hello");
      } catch (ArithmeticException e) {
         System.out.println("you cannot divide a number with zero");
      }
   }
}

Output

you cannot divide a number with zero
raja
Published on 11-Jan-2018 06:13:02
Advertisements