Consider the following code snippets:
public class Geeksforgeeks { public static void main(String[] args) { double p = 1; System.out.println(p/0); } } |
Output:
Infinity
public class Geeksforgeeks { public static void main(String[] args) { int p = 1; System.out.println(p/0); } } |
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.
This article is contributed by Pranjal Mathur. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- User-defined Custom Exception in Java
- Understanding Array IndexOutofbounds Exception in Java
- Null Pointer Exception In Java
- Types of Exception in Java with Examples
- Java | Exception Handling | Question 1
- Java | Exception Handling | Question 2
- Java | Exception Handling | Question 3
- Java | Exception Handling | Question 4
- Java | Exception Handling | Question 8
- Java | Exception Handling | Question 6
- Java | Exception Handling | Question 7
- Java | Exception Handling | Question 8
- 3 Different ways to print Exception messages in Java
- Output of Java program | Set 12(Exception Handling)
- Understanding OutOfMemoryError Exception in Java
- Exception Propagation in Java
- Nested try blocks in Exception Handling in Java
- Exception Handling with Method Overriding in Java
- Version Enhancements in Exception Handling introduced in Java SE 7
- Comparison of Exception Handling in C++ and Java

