常见的下溢和溢出异常

编程入门 行业动态 更新时间:2024-10-11 17:19:16
本文介绍了常见的下溢和溢出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图在java中获得一个溢出和下溢的异常,但是不能得到任何漂亮的教程。具体来说,我想学习

  • 他们有什么不同?
  • 什么是子类这些例外情况?
  • 在哪种情况下会抛出?
  • 哪些可以被处理,如何处理?
  • 与他们相关的最佳做法是什么?
  • 任何有用的教程链接都会执行

    解决方案

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

  • 当一个数字太大而不适合其值类型时,会发生算术溢出。例如, int 保存在-2 31 和2 31 -1之间的值。如果您的号码超过这些限制,则会发生溢出,并且数字包围。这些不会导致在Java中生成异常。
  • 当浮点数太小而无法从零中区分(数字的精度被截断)时,会发生算术下溢。在Java中,这些也不会引起异常。
  • 当您调用一个函数,调用另一个函数,然后调用另一个函数,然后调用另一个函数,然后调用另一个函数,然后调用函数调用堆栈太深了。当发生这种情况时,您会收到一个 StackOverflowError 。
  • 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

  • How are they different from each other?
  • What are the subclasses of these exceptions?
  • In which scenario they are thrown?
  • Which of them can be handled and how?
  • 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....

  • 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.
  • 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.
  • 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.
  • 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.

    更多推荐

    常见的下溢和溢出异常

    本文发布于:2023-10-24 20:51:38,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1524971.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:异常   常见

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!