常见的下溢和溢出异常

发布于 2024-08-19 23:35:13 字数 189 浏览 3 评论 0原文

我试图掌握java中的溢出和下溢异常,但无法获得任何好的教程。具体来说,我想了解

  1. 它们有何不同?
  2. 这些异常的子类是什么?
  3. 它们在什么情况下被抛出?
  4. 其中哪些可以处理以及如何处理?
  5. 与它们相关的最佳实践是什么?

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

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

  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?

Any link to useful tutorial will do

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绝對不後悔。 2024-08-26 23:35:14

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

  1. 当数字变得太大而无法适应其值类型时,就会发生算术溢出 例如,int 保存的值介于 -231 和 231-1(含)之间。如果您的号码超过这些限制,则会发生溢出,并且号码会“环绕”。这些不会导致 Java 中生成异常。
  2. 当浮点数变得太小而无法很好地区分零时(数字的精度被截断),就会发生算术下溢。在 Java 中,这些也不会导致异常。
  3. 当您调用一个函数,调用另一个函数,然后调用另一个函数,然后调用另一个函数......并且函数调用堆栈变得太深时,就会发生堆栈溢出。发生这种情况时,您会收到 StackOverflowError 。
  4. Java 中不会发生堆栈下溢。它的运行时系统应该可以防止此类事情发生。

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

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

  1. Arithmetic overflow happens when a number gets too big to fit in its value type. For example, an int holds values between -231 and 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 StackOverflowError when that happens.
  4. Stack underflow doesn't happen in Java. Its runtime system is supposed to prevent that sort of stuff from happening.

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

世态炎凉 2024-08-26 23:35:14

在 Java 算术中,上溢或下溢将永远抛出异常。相反,对于浮点算术,该值设置为 不是数字、“无限”或零。

要测试这些,您可以使用静态方法: 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");
}

对于某些操作,您可以获得 ArithmeticException,例如在整数数学中除以零时。

我刚刚问了一个关于处理此问题的完整项目风格方法的相关问题

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

To test for these you can use the static methods: isNaN or isInfinite using the appropriate wrapper classes. You can handle this as appropriate. Example:

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 zero in Integer maths.

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

已下线请稍等 2024-08-26 23:35:14

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); }
}

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

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); }
}
烟火散人牵绊 2024-08-26 23:35:14

由于这是一个非常老的问题,需要注意的是,从 Java 8 开始,Math 包提供了“精确”的算术方法,当发生溢出时会触发异常。

例如

int iv1 = Integer.MAX_VALUE;
int iv2 = Math.addExact(iv1,1);

将触发 java.lang.ArithmeticException: integer Overflow 。

Since this is a very old question, it should be noted that since Java 8, the Math package provides "exact" arithmetic methods which will trigger exceptions when overflow occurs.

e.g.

int iv1 = Integer.MAX_VALUE;
int iv2 = Math.addExact(iv1,1);

will trigger java.lang.ArithmeticException: integer overflow .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文