If I pass Int32.MinValue and -1 into the Divide()
method, I get a System.OverflowException
despite the block happening in an unchecked
block.
private static int Divide(int n, int d)
{
return unchecked (n / d);
}
This is surprising to me - unless I've read the documentation for checked / unchecked incorrectly, I'd expect it to just give me an overflowed output (since Int32.MinValue / -1 = 2^31 = Int32.MaxValue + 1, I was expecting an overflow to a value of Int32.MinValue). Instead it threw an OverflowException
.
Here's a DotNetFiddle showing the issue.