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

后端 未结 4 1771
刺人心
刺人心 2020-12-01 19:52

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.

Wh

4条回答
  •  鱼传尺愫
    2020-12-01 20:13

    Java was based on C and C++ which is based on Assembly. In none of these languages is an exception thrown, partly because Exceptions were not in those languages when the arithmetic operations were designed.

    It is much safer to use a larger type like long or double or BigInteger or BigDecimal in the first place and only use smaller types if you are really sure this is inappropriate.

    It doesn't have to be a common problem if you use these wider types.

提交回复
热议问题