Questions tagged [arithmeticexception]

An ArithmeticException is an Exception that indicates an error generated when performing a calculation.

An ArithmeticException is an Exception that indicates an error generated when performing a calculation.

A common calculation that throws this exception is trying to divide a number by zero, such as this...

double firstNumber = 10;
double secondNumber = input.readDouble();
double result = firstNumber / secondNumber;

When coding such a calculation, it is easy to overlook the fact that the user can input a zero for the secondNumber. For the vast majority of numbers, this code will run correctly, and so the programmer might overlook the potential problem.

Programmers should wrap calculation code within try-catch blocks to help catch these Exceptions, or perform validation of variables before they're used in a calculation.

64 questions
-2
votes
1 answer

Java is not trowing Arithmetic exception

I have this Java code: public class Calc { public int quotient(int a, int b){ return a/b; } } and TestNG unit test for this method: @Test () public void testingMethod3() { Assert.assertEquals(0, calc.quotient(5,0)); } On my…
Sančiezz
  • 81
  • 2
  • 11
-3
votes
1 answer

Why does (int)(2/0.9) = 2?

I put this into java and I was given the result 2. Was wondering why it isn't an ArithmeticException, as shouldn't (int)(2/0.9) turn into 2/0. All help is appreciated.
-4
votes
1 answer

Why try-catch is not giving the desired output?

The following code gives the desired output which is "Arithmetic Exception occurs" public static void main(String[] args) { try { int []a = new int[5]; a[5] = 30/0; } catch(ArithmeticException e) { …
-4
votes
1 answer

Why arithmeticexception is unchecked exception in java

Why arithmeticexception is unchecked exception and why we define 2 types of exception Unchecked and Checked in Java?
1 2 3 4
5