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
634
votes
9 answers

ArithmeticException: "Non-terminating decimal expansion; no exact representable decimal result"

Why does the following code raise the exception shown below? BigDecimal a = new BigDecimal("1.6"); BigDecimal b = new BigDecimal("9.2"); a.divide(b) // results in the following exception. Exception: java.lang.ArithmeticException: Non-terminating…
Jason
  • 12,229
  • 20
  • 51
  • 66
67
votes
6 answers

Why does division by zero with floating point (or double precision) numbers not throw java.lang.ArithmeticException: / by zero in Java

The following statement throws java.lang.ArithmeticException: / by zero as obvious. System.out.println(0/0); because the literal 0 is considered to be an int literal and divide by zero is not allowed in integer arithmetic. The following case…
Tiny
  • 27,221
  • 105
  • 339
  • 599
60
votes
8 answers

Java division by zero doesn't throw an ArithmeticException - why?

Why doesn't this code throw an ArithmeticException? Take a look: public class NewClass { public static void main(String[] args) { // TODO code application logic here double tab[] = {1.2, 3.4, 0.0, 5.6}; try { …
Katie
  • 3,517
  • 11
  • 36
  • 49
44
votes
5 answers

How to truncate a BigDecimal without rounding

After a series of calculations in my code, I have a BigDecimal with value 0.01954 I then need to multiply this BigDecimal by 100 and I wish the calculated value to be 1.95 I do not wish to perform any rounding up or down, I just want any values…
DJ180
  • 18,724
  • 21
  • 66
  • 117
27
votes
3 answers

How to prevent arithmetic overflow error when using SUM on INT column?

I am using SQL Server 2008 R2 and I have an INT column where the data inserted never surpasses the max INT, but I have a query which uses the SUM function which when executed surpasses the max INT limit and throws the error mentioned in the title. I…
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
20
votes
3 answers

ASP.NET Overflow or underflow in the arithmetic operation when returning large file bigger 1 GB

I went across some sort of limitation in ASP.NET. I reduced the problem into a sample project in ASP.NET MVC Project (created with Visual Studio 2010 and .NET 4) and the problem still occurs: In a MVC Controller I have a method which provides a file…
Alex
  • 201
  • 1
  • 2
  • 3
13
votes
1 answer

Android BigInteger ArithmeticException

I am trying to implement the RSA-Algorithm in an Android Application. I am using the java.math.BigInteger.modPow() function for the en-/decryption which works fine for my Computer (Windows and Xubuntu) and my Raspberry Pi (also Debian). When the…
qwerty
  • 171
  • 1
  • 11
6
votes
3 answers

Arithmetic Exception in gdb, but I'm not dividing by zero?

I've been getting a Floating point exception (core dumped) error in my C++ program, and gdb shows that the problem is on a line that performs modulo division: Program received signal SIGFPE, Arithmetic exception. [Switching to Thread 0x7ffff6804700…
crognale
  • 171
  • 1
  • 1
  • 6
4
votes
2 answers

Core Java on division by 0.0

I heard one twist in java. If anyone know proper reason you're asked to share it. Question is: double result1 = 1.0/0.0; sop(result1); // output Infinity double result2 = 0.0/0.0; sop(result2); // output NaN the same is happening for float variable…
Sridhar K
  • 37
  • 4
4
votes
2 answers

Big Decimal periodic number

I want to calculate with the java class BigDecimal, but I get always an exception by periodic numbers. I've been looking on the internet but unfortunately found nothing. Maybe someone can help me to fix it. Example: System.out.println(new…
SnowN
  • 53
  • 2
  • 9
4
votes
3 answers

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

I have two questions about Exceptions. Firstly, I got this message from my code... Exception in thread "main" java.lang.ArithmeticException: / by zero This error message means dividing by zero, such as by doing int a = 5 / 0; A method can throw an…
3
votes
0 answers

A fatal error has been detected by the Java Runtime Environment while using arithmetic exception in Mac

In my code I am trying to handle and Arithmetic Exception in eclipse java. My code is running fine in windows but its giving some Fatal error in Mac although it is working fine for other kinds of exception like IndexOutOfBound. And I am getting no…
3
votes
2 answers

Why am I getting java.lang.ArithmeticException: divide by zero

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.marsad.wallpaperapp, PID: 20425 java.lang.ArithmeticException: divide by zero Hi, When I click on gif image to apply as live wallpaper mostly everything works good but sometime app crash with…
Marsad
  • 859
  • 1
  • 14
  • 35
3
votes
1 answer

java MonetaryConversions throws ArithmeticException for high digit currencies

I'd like to use standard java MonetaryConversions to convert currencies. At first glance it works very well and simple: @Test public void testConversion() { FastMoney usd = FastMoney.of(1000, Monetary.getCurrency("USD")); …
3
votes
1 answer

why does ambiguity not raised for ArithmeticException parameter method

Below code executed with out any compilation error of ambiguity, and output is "ArithmeticException". Guys can u help me to know the reason. class Test { public static void main(String[] args) throws UnknownHostException, IOException { …
Siva kumar
  • 215
  • 4
  • 13
1
2 3 4 5