System.OverflowException in unchecked block of C#

will_w

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.

Joe Sewell

From the C# draft specification on integer division:

If the left operand is the smallest representable int or long value and the right operand is -1, an overflow occurs. In a checked context, this causes a System.ArithmeticException (or a subclass thereof) to be thrown. In an unchecked context, it is implementation-defined as to whether a System.ArithmeticException (or a subclass thereof) is thrown or the overflow goes unreported with the resulting value being that of the left operand.

I'm not sure where Microsoft lists its choices for implementation-defined behavior, but apparently they chose the first option here.

This and other implementation-defined or undefined behavior is listed in Annex B of ECMA-334. The draft specification above is updated more recently, but it seems to lack this annex.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

can't java unchecked exceptions be handled using try/catch block?

Get system block size in Java

Isn't an unchecked exception that is caught in a try block a checked exception in Java?

java.io and file system block size

Why dividing int.MinValue by -1 threw OverflowException in unchecked context?

How to do a loop on all unchecked items from checkedlistbox C#?

The way C# represents negative integers in memory and casts them unchecked

Read file block by block in C

OverflowException only in VB.net, not in C#

How to handle System.OverflowException

Why does System.Decimal ignore checked/unchecked context

c# - Show MessageBox if any Checkbox in a DataGridColumn/Row is unchecked

Is the unchecked keyword necessary in the VB.NET version of this C# statement?

System.OverflowException on converting string 0x255 to byte

Why does VS 2017 generate GetHashCode without an unchecked block

C# overflow behavior for unchecked uint

Get unchecked values from CheckBoxList in c#

How unchecked int overflow work c#

How to clear textbox when all checkboxes are unchecked in DataGridView C#

System.IndexOutOfRangeException on input block cipher using c#

How to determine the block size used by the file system by using a C program that performs incremental read()'s?

Is there a block-level storage file system?

How to implement automatically checked and unchecked radio button and timer in C#?

Run an event when Checkbox is Checked/Unchecked Gridview C#

ureadahead seems to block the system on boot

System.OverflowException: "The arithmetic operation caused an overflow." VB.NET

C# System.OverflowException Error in loops

Does C# throw OverflowException for floating point numbers?

C# adding to a list when two out of two radios are unchecked