GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||
GeezerVison is showing www.tutorialspoint.com — Java - ArithmeticException
Selected Reading
Java - ArithmeticExceptionThe Java ArithmeticException throws an error when a wrong mathematical operation is performed. In java, ArithmeticException is present in Java.lang package and the base class of this exception is java.lang.ArithmeticException. JVM raises an Arithmetic exception when the user tries to perform any arithmetic operations that are not possible. There are some scenarios when JVM throws an Arithmetic exception in Java, two of them are following:
Constructors of ArithmeticExceptionThere are two constructors of ArithmeticException class:
Methods of ArithmeticExceptionThere are some methods of ArithmeticException class:
Example of ArithmeticExceptionIn this example, we are dividing a number by zero, so JVM will throw an ArithmeticException. public class ArithmeticExceptionExample {
public static void main(String[] args) {
int a = 10;
int b = 0;
int c = a/b;
System.out.println(c);
}
}
OutputFollowing is the output of the above code: Exception in thread "main" java.lang.ArithmeticException: / by zero
at ArithmeticExceptionExample.main(ArithmeticExceptionExample.java:5)
As you can see in the output, JVM throws an ArithmeticException because we are dividing a number by zero. Handling ArithmeticExceptionIn this example, we are dividing a number by zero, so JVM will throw an ArithmeticException. We are handling this exception using try-catch block. public class ArithmeticExceptionExample {
public static void main(String[] args) {
int a = 10;
int b = 0;
try {
int c = a/b;
System.out.println(c);
} catch(ArithmeticException e) {
System.out.println("Can't divide a number by zero");
}
}
}
OutputFollowing is the output of the above code: Can't divide a number by zero As you can see in the output, we are handling the ArithmeticException using try-catch block. Throwing ArithmeticException explicitlyWe will create an Arithmetic exception explicitly using the throw keyword. We can create a custom error using the throw keyword. This keyword is used to throw the exception for a method. It cannot throw multiple exceptions. In the following example, we will throw the exception explicitly using the throw keyword. We will declare and initialize an array. Within the if block, if the sum of the 1st index and the 2nd index is less than 0 then it throws an exception otherwise print the value. public class ArithmeticExceptionstring {
public static void main(String[] args) {
int [] arr = {4, -12, 6, 8};
for (int i = 0; i < arr.length; i++){
if (arr[0]+arr[1] < 0){
throw new ArithmeticException ("Negative value");
}else{
System.out.println (arr[0]+arr[1]);
break;
}
}
}
}
OutputFollowing is the output of the above code: Exception in thread "main" java.lang.ArithmeticException: Negative value
at ArithmeticExceptionstring.main(ArithmeticExceptionstring.java:6)
As you can see in the output, we are throwing an ArithmeticException explicitly using the throw keyword. java_lang_exceptions.htm Advertisements
| ||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |