常见的下溢和溢出异常 [英] Common underflow and overflow exceptions

查看:262
本文介绍了常见的下溢和溢出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在java中获得一个溢出和下溢的异常,但是不能得到任何漂亮的教程。具体来说,我想学习


  1. 他们有什么不同?

  2. 什么是子类这些例外情况?

  3. 在哪种情况下会抛出?

  4. 哪些可以被处理,如何处理?

  5. 与他们相关的最佳做法是什么?

任何有用的教程链接都会执行

解决方案

好吧,OP谈到想知道堆栈溢出和算术溢出以及它们相应的下溢。这里... ....


  1. 当一个数字太大而不适合其值类型时,会发生算术溢出。例如, int 保存在-2 31 和2 31 -1之间的值。如果您的号码超过这些限制,则会发生溢出,并且数字包围。这些不会导致在Java中生成异常。

  2. 当浮点数太小而无法从零中区分(数字的精度被截断)时,会发生算术下溢。在Java中,这些也不会引起异常。

  3. 当您调用一个函数,调用另一个函数,然后调用另一个函数,然后调用另一个函数,然后调用另一个函数,然后调用函数调用堆栈太深了。当发生这种情况时,您会收到一个 StackOverflowError

  4. Java中不会发生堆栈下溢。它的运行时系统应该防止这种情况发生。

要回答OP的其他问题(见评论),当你超过数组的边界,发出 IndexOutOfBoundsException


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

解决方案

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.

这篇关于常见的下溢和溢出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆