Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why, In Java arithmetic, overflow or underflow will never throw an Exception?

During Java Arithmetic operation , JVM do not throw Underflow or Overflow Exception. So many time we come across unexpected results and wondering what went wrong.

While in case of .NET technology we have Overflow and Undeflow exception.

So my question is that , why Java was design not to throw this exception during arithmetic operation

like image 971
Alpesh Gediya Avatar asked Apr 18 '13 14:04

Alpesh Gediya


People also ask

Does Java throw overflow exception?

Java does not throw an exception when an overflow occurs; that is why it can be hard to find errors resulting from an overflow. Nor can we directly access the overflow flag, which is available in most CPUs.

Is arithmetic overflow an exception?

An OverflowException is thrown at run time under the following conditions: An arithmetic operation produces a result that is outside the range of the data type returned by the operation.

What happens when arithmetic overflow?

An arithmetic overflow is the result of a calculation that exceeds the memory space designated to hold it. For example, a divide-by-zero yields a much larger result.

What is arithmetic overflow and underflow?

Storing values that are too low in an integer variable (e.g., attempting to store −1 in an unsigned integer) is properly referred to as integer overflow, or more broadly, integer wraparound. The term underflow normally refers to floating point numbers only, which is a separate issue.


2 Answers

This was likely a combination of factors:

  1. The big languages prior to Java used unchecked arithmetic. Well-known algorithms prone to numerical overflow tended to account for the potential overflow already without relying on checked arithmetic.
  2. Checked arithmetic introduces significant overhead in algorithms making heavy use of arithmetic instructions, which would put Java at a substantial disadvantage especially for benchmarks.
  3. Some algorithms rely on silent numerical overflow/underflow. If arithmetic operations were checked, rewriting these algorithms could quickly become non-trivial.
  4. Checked arithmetic is not necessary to ensure memory safety (as opposed to other JVM checks like null pointers and array bounds).

The .NET virtual execution environment (now part of the ECMA-335 standard) introduced separate instructions for checked and unchecked arithmetic, allowing it to independently address the performance and safety concerns of developers working in modern managed languages.

like image 131
Sam Harwell Avatar answered Oct 20 '22 03:10

Sam Harwell


It probably has to do with performance as Indoknight said. Java provided the tools to care for overflow, so if you need to detect it you could do it. You also have long and BigInteger and can use those to avoid your int overflows.

You should see this answer from a similar question in stackoverflow. How does Java handle integer underflows and overflows and how would you check for it?

like image 30
Mr. Adobo Avatar answered Oct 20 '22 04:10

Mr. Adobo