Common underflow and overflow exceptions

35,961

Solution 1

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.

Solution 2

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.

Share:
35,961

Related videos on Youtube

Ravi Gupta
Author by

Ravi Gupta

Developer

Updated on July 09, 2022

Comments

  • Ravi Gupta
    Ravi Gupta almost 2 years

    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

    • C. K. Young
      C. K. Young over 14 years
      I wasn't the person who downvoted you, but, what kind of overflow/underflow are you talking about? Arithmetic? Stack? Something else?
  • C. K. Young
    C. K. Young over 14 years
    -1 100 / 0 actually returns infinity, not a NaN. Also, an arithmetic underflow (denormal) just returns 0.
  • Ravi Gupta
    Ravi Gupta over 14 years
    Thanks, I am curios about exceptions like BufferOverflowException and BufferUnderflowException and related stuff
  • Nate
    Nate over 14 years
    @Ravi Gupta - please add that to the main question - BufferOverflowException and BufferUnderflowException is a lot more specific than your original vague "overflow and underflow exceptions" question.
  • C. K. Young
    C. K. Young over 14 years
    @Ravi: There is no BufferOverflowException or anything like that. Arrays are accessed by index, which, when the index becomes invalid, causes an IndexOutOfBoundsException.
  • Paolo
    Paolo over 14 years
    +1 clear explanation. I've also heard subtracting one from int.MIN_VALUE called an underflow. I've not come across your point 2 before.
  • C. K. Young
    C. K. Young over 14 years
    @Paolo: I think subtracting one from Integer.MIN_VALUE is considered an overflow too. But maybe I'm splitting hairs here. :-)
  • Ravi Gupta
    Ravi Gupta over 14 years
    @Chris: They have something like BufferOverflowException java.sun.com/j2se/1.5.0/docs/api/java/nio/…
  • C. K. Young
    C. K. Young over 14 years
    Oh! Overflowing a ByteBuffer. That's something else entirely. Lemme edit my answer. :-P
  • Ravi Gupta
    Ravi Gupta over 14 years
    @Chris Thanks, it helps, where can I get documentation for this stuff ?
  • C. K. Young
    C. K. Young over 14 years
    @Ravi: Wow, I'd be hard-pressed to give you specific references at the moment, because a lot of this actually came from personal experience (I've programmed in Java for over 5 years). Hmm. You can (and should) try to read the Java Language Specification, if you're interested in the nitty-gritty.
  • Ravi Gupta
    Ravi Gupta over 14 years
    @Chris I was trying hunt it on sun's site but couldn't get what I was looking. I will keep on looking, if I got it will post it here
  • Andreas Dolk
    Andreas Dolk over 14 years
    @Feast, this is partially incorrect: 'Integer.MAX_VALUE + 2' results in '-2147483647' - No exception but not NaN or infinite or zero either. 'Double.MAX_VALUE * 2' will indeed result in 'Infinity' (which is from a mathematical perspective a pretty wrong answer ;) )
  • Pool
    Pool over 14 years
    Thanks, I've "divided" the answer into float and int sections.
  • Jool
    Jool about 3 years
    Overflow occurs when the number is too big to fit into the type, causing the negative bit indicator to be set, and since Java stores negatives as "two's complement", this will look like a large negative number. Adding 1 to 2147483647 (int max_value) thus becomes -2147483648.

Related