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

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.

Leave a Comment