

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Infinity or exception in Java when divide by 0?
Consider the following code snippet where we divide a number by 0.
Example
public class Tester{ public static void main(String[] args) { double d = 100; System.out.println(d/0); } }
Output
Infinity
Now consider the following code snippet.
Example
public class Tester{ public static void main(String[] args) { int d = 100; System.out.println(d/0); } }
Output
Exception in thread "main" java.lang.ArithmeticException: / by zero at Tester.main(Tester.java:5)
As you've noted, the Infinity vs ArithmeticException, a different result for similar divide by zero program. The difference lies in floating point arithmetic used in first program and integer arithmetic used in second program.
- Related Questions & Answers
- Infinity or Exception in C# when divide by 0?
- How to capture divide by zero exception in Java?
- Handling the Divide by Zero Exception in C++
- How to capture divide by zero exception in C#?
- How to divide matrix rows by maximum value in each row excluding 0 in R?
- When should we create a user-defined exception class in Java?
- How to divide data.table object rows by maximum value in each row excluding 0 in R?
- MySQL IF/WHEN/ELSE/OR with ORDER BY FIELD
- How to divide data frame row values by maximum value in each row excluding 0 in R?
- Are the instances of Exception checked or unchecked exceptions in java?
- Divide one polynomial by another in Python
- Chained exception in Java
- Exception propagation in Java
- Exception chaining in Java
- Test element-wise for positive or negative infinity in Numpy
Advertisements