Why doesn't this produce an overflow exception

2019-04-08 20:00发布

问题:

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?

回答1:

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.



回答2:

"For the arithmetic, casting, or conversion operation to throw an OverflowException, the operation must occur in a checked context."

The OverflowException will be called as soon as you put your code within a checked block.

ulong lSmallValue = 5;
ulong lBigValue = 10;
checked {
try {
    ulong lDifference = lSmallValue - lBigValue;
}
catch (OverflowException) {
    Console.WriteLine("Exception caught");
}}