Java 常见的下溢和上溢异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2154712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:12:12  来源:igfitidea点击:

Common underflow and overflow exceptions

javaexceptionoverflowunderflow

提问by Ravi Gupta

I am trying to get a hold of overflow and underflow exceptions in java, but couldn't get any nice tutorial. Specifically I wish to learn

我试图在 Java 中掌握溢出和下溢异常,但找不到任何好的教程。具体我想学习

  1. How are they different from each other?
  2. What are the subclasses of these exceptions?
  3. In which scenario they are thrown?
  4. Which of them can be handled and how?
  5. What are the best practice related to them?
  1. 它们之间有何不同?
  2. 这些异常的子类是什么?
  3. 在哪种情况下会抛出它们?
  4. 哪些可以处理?如何处理?
  5. 与它们相关的最佳实践是什么?

Any link to useful tutorial will do

任何有用教程的链接都可以

采纳答案by Chris Jester-Young

Okay, the OP talked about wanting to know about both stack overflow and arithmetic overflow, as well as their corresponding underflow. Here goes....

好的,OP 谈到想要了解堆栈溢出和算术溢出,以及它们相应的下溢。开始....

  1. Arithmetic overflow happens when a number gets too big to fit in its value type. For example, an intholds values between -231and 231-1, inclusive. If your number goes over these limits, an overflow occurs, and the number "wraps around". These do not cause an exception to be generated in Java.
  2. Arithmetic underflow happens when a floating point number gets too small to distinguish very well from zero (the precision of the number got truncated). In Java, these do not cause an exception either.
  3. Stack overflow happens when you call a function, that calls another function, that then calls another, then another...and the function call stack gets too deep. You get a StackOverflowErrorwhen that happens.
  4. Stack underflow doesn't happen in Java. Its runtime system is supposed to prevent that sort of stuff from happening.
  1. 当一个数字太大而无法放入其值类型时,就会发生算术溢出。例如, anint包含 -2 31和 2 31-1之间的值,包括这两个值。如果您的号码超过这些限制,则会发生溢出,并且号码会“环绕”。这些不会导致在 Java 中生成异常。
  2. 当浮点数太小而无法与零区分开时(数字的精度被截断),就会发生算术下溢。在 Java 中,这些也不会导致异常。
  3. 当你调用一个函数,调用另一个函数,然后调用另一个,然后另一个......并且函数调用堆栈变得太深时,就会发生堆栈溢出。StackOverflowError当这种情况发生时,你会得到一个。
  4. Java 中不会发生堆栈下溢。它的运行时系统应该可以防止这种事情发生。

To answer the OP's other question (see comments), when you overstep the boundaries of an array, an IndexOutOfBoundsExceptionis issued.

要回答 OP 的其他问题(请参阅评论),当您超出数组的边界时,IndexOutOfBoundsException会发出an 。

回答by Pool

In Java arithmetic, overflow or underflow will neverthrow an Exception. Instead, for floating point arithmetic the value is set as Not a number, 'infinite' or zero.

在 Java 算术中,上溢或下溢永远不会抛出异常。相反,对于浮点算术,该值被设置为Not a number, 'infinite' 或 0。

To test for these you can use the static methods: isNaNor isInfiniteusing the appropriate wrapper classes. You can handle this as appropriate. Example:

要测试这些,您可以使用静态方法:isNaNisInfinite使用适当的包装类。您可以酌情处理。例子:

double d1 = 100 / 0.;
if (Double.isNaN(d1)) {
    throw new RuntimeException("d1 is not a number");
}
if (Double.isInfinite(d1)) {
    throw new RuntimeException("d1 is infinite");
}

For certain operations you can get an ArithmeticException, for example when dividing by zeroin Integer maths.

对于某些操作,您可以获得ArithmeticException,例如dividing by zero在整数数学中。

I just asked a related questionabout a complete project style way to handle this.

我刚刚问了一个有关处理此问题的完整项目样式方式的相关问题

回答by ceving

Java has no unsigned integers. This makes it easy to throw an Exception, if you think it might be useful.

Java 没有无符号整数。如果您认为它可能有用,这使得抛出异常变得容易。

public class Counter
{
  private int counter = 0;

  public int increment ()
  {
    counter += 1;
    if (counter < 0)
      throw new RuntimeException ("Counter overflow");
    else
      return counter;
  }

  public String toString() { return String.valueOf(counter); }
}