1

This is for Java. I understand these terms and how they go over a variable storage limit it wraps around and becomes positive if the number was negative and vice versa.

I am having trouble getting these exceptions to be thrown.

this is the method.

// computes a + band saves result in answer
public void add (int a, int b)   

I've tried adding 2,147,483,647 + 1 and-2,147,483,648 -1

and even dividing it but it doesnt give me an exception.

does anyone know what I am doing wrong?

Francesco Casula
  • 26,184
  • 15
  • 132
  • 131
JavaNoob
  • 41
  • 5
  • According to this answer: http://stackoverflow.com/a/2154843/2127454 such situations will not cause Exception to be thrown – Keammoort Dec 03 '15 at 13:02
  • nothing, every number acts the same by default for an `Overflow`. that beeing `Number.MAX_VALUE + 1 = Number.MIN_VALUE` and `Number.MIN_VALUE - 1 = Number.MAX_VALUE` – SomeJavaGuy Dec 03 '15 at 13:03
  • 2
    Possible duplicate of [Common underflow and overflow exceptions](http://stackoverflow.com/questions/2154712/common-underflow-and-overflow-exceptions) – Adil Dec 03 '15 at 13:03
  • but my lab question asks "so that it causes an Overflow exception to be thrown when the sum of two positive integers is negative . Write the corresponding exception class Overflow" – JavaNoob Dec 03 '15 at 13:16
  • not really sure how to do that with that method :/ – JavaNoob Dec 03 '15 at 13:16
  • nevermind got it :D thanks – JavaNoob Dec 04 '15 at 06:39

2 Answers2

0

I understand these terms and how they go over a variable storage limit it wraps around and becomes positive if the number was negative and vice versa.

Yes. That is what happens. When you add 1 to the largest int value (231 - 1) you get the smallest int value (-231). You get analogous behavior for byte, short, long and even char.

In the case of float and double overflow and underflow results in an "infinity" value.

I am having trouble getting these exceptions to be thrown.

Firstly, "those" exceptions don't exist.

Secondly, no exceptions are thrown when integer or floating point arithmetic overflows or underflows.

The only cases where arithmetic gives an exception are integer division by zero and remainder zero ... both of which throw an ArithmeticException.


but my lab question asks "so that it causes an Overflow exception to be thrown when the sum of two positive integers is negative . Write the corresponding exception class Overflow"

It is asking you to declare a custom OverflowException class, and code your addition method to throw that exception when the result overflows. You have to code the logic for overflow detection yourself.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

This unusual result comes about due to the way the data type int is stored in Java and how Java handles calculations that go beyond the storage capacity.
Recall that the range of type int is -2,147,483,648 to +2,147,483,647 inclusive.You might assume that the addition 2,147,483,647 + 1 causes a runtime (integer overflow) error. This is not so because Java uses a technique called two's complement to represent integers;
So numbers larger than 2,147,483,647 "wrap around" to negative values, while numbers smaller than -2,147,483,648 "wrap around" the other way to positive values.

That is 2,147,483,647 + 1 = -2,147,483,648 and -2,147,483,648 - 1 = 2,147,483,647 .

Consequently, integer addition never throws a runtime exception. In some situations, it might be preferable if integer addition did throw overflow and underflow exceptions. Otherwise, a logical bug might go undetected.

Rahul Sharma
  • 5,614
  • 10
  • 57
  • 91