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
1
vote
2 answers

Why is it implying I'm dividing by zero?

I'm solving a few exercises from HackerRank.com and the code works perfectly on Netbeans and even in the page compiler for test cases, but when I submit the code it throws me this error in every test(except the last): ArithmeticException: thrown at…
plethora
  • 135
  • 1
  • 8
1
vote
3 answers

Certain BigDecimal numbers in Java when divided causes the ArithmeticException to be thrown

The following approach to divide two BegDecimal numbers works fine. BigDecimal a=new BigDecimal(5); BigDecimal b=new BigDecimal(2); System.out.println(a.divide(b)); Output : 2.5 The following same approach however fails with the…
Tiny
  • 27,221
  • 105
  • 339
  • 599
0
votes
2 answers

Division by zero doesn't always throw java.lang.ArithmeticException

Why does it show Infinity instead of throwing the exception? Integer Class : public class Entier { int A; public Entier(int A){ this.A = A; } public double division(Entier diviseur){ return (double) this.A /…
user18070500
0
votes
0 answers

ArithmeticException not being caught

I have the following method defined in one of my classes: public String calculateProfitMargin() { String returnMe = ""; double profitMargin = 0; try { profitMargin = (orderItems[0].getItem().getPriceSoldFor() -…
0
votes
0 answers

Why overriding a parent class method and adding throws statement in a child class generating an compile time error in java

I have two codes. In first code I am getting compile time error in java and second works fine . Why is it happening so **First Code ** import java.io.*; class Parent{ void msg(){System.out.println("parent");} } class TestExceptionChild…
0
votes
2 answers

Arithmetic exception in C

Arithmetic Exception //m*n行列Aを用いてy = A*x +b を計算する void fc(int m, int n, const float *x, const float *A, const float *b, float *y){ int i, j; for (i = 0; i < m; i++){ y[i] = b[i]; for (j = 0; j < n; j++){ y[i] +=…
MBH
  • 11
  • 3
0
votes
1 answer

Exception in thread awt-eventqueue-2 java.lang.ArithmeticException / by Zero

my app seems to works fine but in some computers I receive that exception. the program check for level 2 permission and then launch a JTable with two tab. The problem is that I'm not able to replicate the problem in my netbeans computer. Any…
Magobin
  • 93
  • 2
  • 13
0
votes
0 answers

Fortran 90 GDB signal SIGFPE, Arithmetic exception

I am trying to debug with GDB, and am encountering an arithmetic error when trying to compare two values. The first value is set at the top of the module as double precision, parameter, public :: Dint = -1.D99 The second value happens to be inta =…
Jacob Shirley
  • 63
  • 1
  • 4
0
votes
2 answers

Cannot work around ArithmeticException? / by zero?

I'm having trouble working around an ArithmeticException I'm getting from my getAverageScore() method in my Student class. I'm trying to write a program that reads the following text file scores.txt: 34 c081 c082 c083 c084 S2023 99 75 85 …
Jaimee-lee Lincoln
  • 365
  • 1
  • 3
  • 11
0
votes
3 answers

why to use multiple catches in exception?

when we can handle it in single default Exception then Why we should use Multiple Catches? public static void main(String[] args) { try{ int a[]=new int[5]; a[5]=30/0; } …
0
votes
2 answers

Not a Number (NaN)

Why some numbers are defined as not a number(NaN) in floating point arithmetic? (Although they can be represented by IEEE format and indeed are real numbers)
0
votes
3 answers

ArithmeticException not thrown for overflow within 2^32

I know assigning a number greater than 2^32 has a chance to generate an ArithmeticException but today while I was programming: int x = 65535 System.out.println(x * x); Output: -131071 So no exception but an unexpected result.
Henry Li
  • 174
  • 2
  • 16
0
votes
1 answer

In java how it is possible to catch an exception in a class and throw it to another class to get/print the type of exception

Suppose class Example1 { public static void main(String args[]) { try{ int num1=30, num2=0; int output=num1/num2; System.out.println ("Result: "+output); } catch(Exception e){ //and in want to determine the type of…
Anindyo Bose
  • 153
  • 2
  • 11
0
votes
1 answer

not getting how to forward 404 page when exception occured

I am trying to handle ArithmeticException in servlet, catch block is being execute but not forwarding to error page. I am getting ArithmeticException: / by zero. Can someone tell why it's not forwarded to general-errorpage.jsp web.xml…
Bharath Pateru
  • 99
  • 1
  • 2
  • 17
0
votes
1 answer

How to figure out which value is 0?

I got my 1st crash report for my app. It says there is a java.lang.ArithmeticException: divide by zero The exception is on one of these two lines: //sWP stands for "Screen Width in Pixels" // sW stands for "Screen Width" The code is old though, so…