VOOZH about

URL: https://www.geeksforgeeks.org/java/g-fact-33-infinity-or-exception/

⇱ Infinity or Exception in Java when divide by 0? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Infinity or Exception in Java when divide by 0?

Last Updated : 6 Feb, 2019
Consider the following code snippets: Output:
Infinity
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
 at Geeksforgeeks.main(Geeksforgeeks.java:8)
Explanation: In the first piece of code, a double value is being divided by 0 while in the other case an integer value is being divide by 0. However the solution for both of them differs.
  • In case of double/float division, the output is Infinity, the basic reason behind that it implements the floating point arithmetic algorithm which specifies a special values like "Not a number" OR “infinity” for “divided by zero cases” as per IEEE 754 standards.
  • In case of integer division, it throws ArithmeticException.
Comment
Article Tags: