The exception that is thrown when an arithmetic, casting, or conversion operation in a checked context results in an overflow.
Questions tagged [overflowexception]
78 questions
49
votes
3 answers
Why dividing int.MinValue by -1 threw OverflowException in unchecked context?
int y = -2147483648;
int z = unchecked(y / -1);
The second line causes an OverflowException. Shouldn't unchecked prevent this?
For example:
int y = -2147483648;
int z = unchecked(y * 2);
doesn't cause an exception.

Marcel Niehüsener
- 651
- 5
- 15
31
votes
8 answers
Arithmetic operation resulted in an overflow. (Adding integers)
I can't understand this error:
In this call to method SetVolume, Volume = 2055786000 and size = 93552000. Volume is an Integer property, and size is also Integer, as you can see.
The class is a partial class of a dbml entity class, however this…

bretddog
- 5,411
- 11
- 63
- 111
22
votes
2 answers
Why does the compiler evaluate remainder MinValue % -1 different than runtime?
I think this looks like a bug in the C# compiler.
Consider this code (inside a method):
const long dividend = long.MinValue;
const long divisor = -1L;
Console.WriteLine(dividend % divisor);
It compiles with no errors (or warnings). Seems like a…

Jeppe Stig Nielsen
- 60,409
- 11
- 110
- 181
11
votes
1 answer
How can Decimal.Round() throw OverflowException
I'm using
Decimal.Round(decimal d)
MSDN says it can throw OverflowException
https://msdn.microsoft.com/en-us/library/k4e2bye2(v=vs.110).aspx
I'm not sure how that can happen. I tried looking over the implementation using ilSpy
And got until the…

Amir Katz
- 1,027
- 1
- 10
- 24
11
votes
2 answers
Oracle number to C# decimal
I know there are several threads and posts regarding this issue in the internet and I've read them (not every article, I have to admit) but none of them did fully satisfy me.
My situation:
I'm using ODP.net (dll version 2.111.6.0) to access the…

robert.oh.
- 644
- 2
- 5
- 13
11
votes
11 answers
How to convert unsigned integer to signed integer without OverflowException
I would like to be able to convert a high-valued unsigned-integer (a value that uses the highest-order bit) to a signed-integer. In this case, I don't care that the value is higher than the maximum value of the signed integer type. I just want it…

Steven Doggart
- 43,358
- 8
- 68
- 105
8
votes
1 answer
Meaning of error numbers in Python exceptions
Catching Python's OverflowError after some dumb calculation, I checked the error's args and saw it's a tuple containing an integer as its first coordinate. I assume this is some kind of error number (errno). However, I could not find any…

Bach
- 6,145
- 7
- 36
- 61
7
votes
1 answer
IntPtr.ToInt32() and x64 systems
In my c# dll I have some code like this to interact with some unmanaged dlls:
IntPtr buffer = ...;
TTPOLYGONHEADER header = (TTPOLYGONHEADER)Marshal.PtrToStructure(
new IntPtr(buffer.ToInt32() + index),…

devdept2
- 71
- 1
- 2
6
votes
3 answers
Rationale behind OverflowException thrown with negative array size?
After writing code that can be boiled down to the following:
var size=-1;
var arr=new byte[size];
I was surprised that it threw an OverflowException. The docs for OverflowException state:
The exception that is thrown when an arithmetic, casting,…

spender
- 117,338
- 33
- 229
- 351
6
votes
1 answer
TaskDialog from WinAPICodePack does not work on .NET 4.0
I am writing an application and wanted to use the TaskDialogIndirect function - however I did not want to write a huge amount of P/Invoke stuff etc. so I have included the WinAPICodePack. There is one problem though! When I create a control for the…

Christian Ivicevic
- 10,071
- 7
- 39
- 74
5
votes
1 answer
Why adding two large integers whose result greater than int.MaxValue doesn't throw overflow exception?
If we are using the following loop in a program, the loop never ends in C# 4.0
for (int i = 1; i <= int.MaxValue; i++)
{
}
This is because adding 1 to int.MaxValue (2147483647) will not result in an overflow exception, but results in -2147483648…

renil
- 409
- 5
- 14
5
votes
1 answer
Why does int.MinValue % -1 cause and OverflowException
In 7.8.3. of the C# Specification regarding the Remainder operator it states the following:
If the left operand is the smallest int or long value and the right
operand is -1, a System.OverflowException is thrown.
Therefore int.MinValue % -1…

Dirk Strauss
- 630
- 1
- 8
- 19
5
votes
2 answers
Why doesn't this produce an overflow exception?
I was testing something out using LinqPad and was surprised that the following code did not produce an exception:
ulong lSmallValue = 5;
ulong lBigValue = 10;
ulong lDifference = lSmallValue -…

Anthony
- 9,451
- 9
- 45
- 72
3
votes
1 answer
Avoiding OverflowException when converting from double to decimal
When converting double (or float) to decimal, overflow exceptions are possible. So I've written this small extension method that prevents this by saturating:
public static decimal ToDecimalSafe(this double input)
{
try
{
return…

relatively_random
- 4,505
- 1
- 26
- 48
3
votes
4 answers
CInt(Long) in VB.NET behaving differently in 32- and 64-bit environments
Today I had a problem converting a Long (Int64) to an Integer (Int32). The problem is that my code was always working in 32-bit environments, but when I try THE SAME executable in a 64-bit computer it crashes with a System.OverflowException…

LocoDelAssembly
- 818
- 8
- 10