Search code examples
c#overflowexception

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 - lBigValue;

Console.WriteLine(lDifference);
Console.WriteLine((long)lDifference);

This produces the following output:

18446744073709551611
-5

Fortunately, I was hoping for this behavior, but I was under the assumption that this would cause an OverflowException to be thrown.

From System.OverflowException:

An OverflowException is thrown at run time under the following conditions:

  • An arithmetic operation produces a result that is outside the range of the data type returned by the operation.
  • A casting or conversion operation attempts to perform a narrowing conversion, and the value of the source data type is outside the range of the target data type.

Why doesn't the operation lSmallValue - lBigValue fall into the first category?


Solution

  • CLR will not throw the Overflow exception by default. Unless you're using the "checked" keyword.

    http://msdn.microsoft.com/en-us/library/74b4xzyw%28v=vs.71%29.aspx

    UPD: Actually, I do recommend the "CLR via C#" by Jeffrey Richter - he makes these things so much more transparent. My favorite book about the CLR and C# fundamentals.