How to capture divide by zero exception in Java?

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


Live Demo

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


you cannot divide a number with zero
Swarali Sree
Swarali Sree

I love thought experiments.