- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- 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#?
- When should we create a user-defined exception class in Java?
- Are the instances of Exception checked or unchecked exceptions in java?
- How to divide matrix rows by maximum value in each row excluding 0 in R?
- Exception propagation in Java
- Exception chaining in Java
- Divide the expression$\frac{523}{0}$
- How to divide data.table object rows by maximum value in each row excluding 0 in R?
- How can an exception be thrown manually by a programmer in java?
- Exception propagation in Java programming
- Chained exception in Java\n
- How to divide data frame row values by maximum value in each row excluding 0 in R?

Advertisements