Java ArithmeticException

KyraThompson's avatar
Published Apr 3, 2023Updated Apr 11, 2023
Contribute to Docs

The ArithmeticException occurs when an arithmetic operation yields an error.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours

Example

The ArithmeticException is thrown in the following example because it divides an integer by zero:

public class DivideByZero {
public static void main(String[] args) {
int x = 9/0;
}
}

The output would look like this:

Exception in thread "main" java.lang.ArithmeticException: / by zero
at DivideByZero.main(DivideByZero.java:3)

All contributors

Contribute to Docs

Learn Java on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours