java.lang.ArithmeticException in java - Divide number by zero

What is hierarchy of java.lang.ArithmeticException?

-java.lang.Object
-java.lang.Throwable
 -java.lang.Exception
  -java.lang.RuntimeException
   -java.lang.ArithmeticException

ArithmeticException is Checked (compile time exceptions) and UnChecked (RuntimeExceptions) in java ?

java.lang.ArithmeticException is a Runtime Exception in java.



What is ArithmeticException in java?

java.lang.ArithmeticException is thrown when some exceptional arithmetic condition occurs in java.


Scenarios where ArithmeticException may be thrown in java >


Dividing Integer number by zero will throw java.lang.ArithmeticException in java.

public class DivideByZero {
public static void main(String[] args) {
     try {
          int x = 1;
          x = x / 0;
     } catch (ArithmeticException e) {
          e.printStackTrace();
     }
}
}

output of above program will be
java.lang.ArithmeticException: / by zero
at DivideByZero.main(DivideByZero.java:5)

No comments:

Post a Comment