Overflow exception for int in C #?

I had this weird experience with issue number 10 on Project Euler (great site, by the way). The challenge was to calculate the sum of all primes below two million.

I used int for the sum, and my algorithm gave the answer, but when I inserted it to check the answer, it was wrong.

It turned out that the result is too large to fit in int, but could it not cause an overflow error or something else? Instead, he simply returned a value far from the real answer.

When I changed the type to long, everything was hunky dory.

+47
c # overflow
Jan 13 '10 at
source share
5 answers

C # integer operations do not throw exceptions when overflowing by default. This can be done using the project settings or by calculating checked :

 int result = checked(largeInt + otherLargeInt); 

Now the operation will be selected.

The opposite is unchecked , which makes any operation clearly unchecked. Obviously, this only makes sense when you have included proven operations in the project settings.

+71
Jan 13 '10 at 12:11
source share

In C # a OverflowException not thrown (in VB, an exception is thrown by default).

To get excpetion, you need to embed your code in the checked context:

 byte value = 241; checked { try { sbyte newValue = (sbyte) value; Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", value.GetType().Name, value, newValue.GetType().Name, newValue); } catch (OverflowException) { Console.WriteLine("Exception: {0} > {1}.", value, SByte.MaxValue); } } 

MSDN explains in more detail:

For arithmetic, casting, or conversion operations, to OverflowException, the operation must occur in a trusted context. From the default, arithmetic operations and overflows are checked in Visual Basic; in C # they are not. If the operation occurs in an uncontrolled context, the result is truncated, discarding any high-order bits that do not fit into the destination type.

+18
Jan 13 '10 at 12:13
source share

This is because, by default, C # does not throw any exceptions for integer overflows, as well as for the lower stream. Here you can do a couple of things.

Option 1

You must allow the exception by going to Project => properties => Insert tab => Advanced => check the arithmetic overflow of the stream (make sure you check the box)

enter image description here

Make sure you check the box

Option 2

Use the marked block and eliminate the overflow exception to handle the situation. An example code snippet would be

  try { checked { int y = 1000000000; short x = (short)y; } } catch (OverflowException ex) { MessageBox.Show("Overflow"); } catch (Exception ex) { MessageBox.Show("Error"); } 

Hope this helps you ... :)

+9
Apr 02 '15 at 8:11
source share

I already added cmt, but this may be interesting for some of you:

msdn tells us:

An integer arithmetic overflow either throws an OverflowException or discards the most significant bits of the result

but

Decimal arithmetic overflow always throws an OverflowException.

and

When an integer overflow occurs, what happens depends on the execution context, which can be checked or unchecked. In the checked context, an OverflowException is thrown. In an uncontrolled context, the most significant bits of the result are discarded and execution continues. So C # gives you the choice of handling or ignoring overflow.

+6
Jan 13 '10 at 12:19
source share

By default, C # does not check the arithmetic overflow of integers. You can change this using the /checked option of the compiler or by enabling "Check arithmetic overflow / underflow" in Visual Studio (project properties - Build - Advanced).

You can use checked and unchecked keywords to override the default value in each case. If you rely on verification performed in a piece of code, I explicitly authorize it to be used with checked . This is a good idea.

 int j = checked(i * 2); checked { int j = i * 2; // Do more stuff } 

Note that floating point operations never throw an OverflowException , and decimal operations always throw an OverflowException . See Also C # Operators .

+6
Jan 13
source share



All Articles