Home Java Beginners Example to throw ArithmeticException in Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Example to throw ArithmeticException in Java

Advertisement
This article discuss what is an arithmetic exception and the why it occurs. Here you'll also learn how to handle such kind of exceptions using example.

Example to throw Arithmetic Exception in Java

Arithmetic Exception is a kind of unchecked error in Java and these kind of exception occurs, when you divide a number by zero or if there is a mathematical or calculation errors that occurs at the RunTime.

java.lang.ArithmeticException is the base class of arithmetic exceptions that extends Runtime Exception.

See the example given below.

Syntax of Arithmetic Exception:

public ArithmeticException()
You can also write an ArithmeticException with the specified detail message, such as:
public ArithmeticException(String s)
Arithmetic Exception Example:
public class StringTest {
public static void divide() throws ArithmeticException {
int x = 10, y = 0;
int z = x / y;
} 
public static void main(String[] args) {
divide();
}
}

Output of the Exception:

Exception in thread "main" java.lang.ArithmeticException: / by zero

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 2, 2013

Ask Questions?    Discuss: Example to throw ArithmeticException in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com