官术网_书友最值得收藏!

Checking for overflow

Earlier, we saw that when casting between number types it was possible to lose information, for example, when casting from a long variable to an int variable. If the value stored in a type is too big, it will overflow.

Add a new Console Application project named Ch03_CheckingForOverflow.

The checked statement

The checked statement tells .NET to throw an exception when an overflow happens instead of allowing to it happen silently.

We set the initial value of an int variable to its maximum value minus one. Then, we increment it several times, outputting its value each time. Note that once x gets above its maximum value, it overflows to its minimum value and continues incrementing from there.

Type the following code in the Main method and run the program:

int x = int.MaxValue - 1;
WriteLine(x);
x++;
WriteLine(x);
x++;
WriteLine(x);
x++;
WriteLine(x);

Press Ctrl + F5 and view the output in the console:

2147483646
2147483647
-2147483648
-2147483647

Now let's get the compiler to warn us about the overflow using the checked statement:

checked
{
    int x = int.MaxValue - 1;
    WriteLine(x);
    x++;
    WriteLine(x);
    x++;
    WriteLine(x);
    x++;
    WriteLine(x);
}

Press Ctrl+F5 and view the output in the console:

2147483646
2147483647
Unhandled Exception: System.OverflowException: Arithmetic operation resulted in an overflow.

Just like any other exception, we could wrap these statements in a try-catch block and display a nicer error message for the user:

try
{
    // previous code goes here
}
catch(OverflowException)
{
    WriteLine("The code overflowed but I caught the exception.");
}

Press Ctrl + F5 and view the output in the console:

2147483646
2147483647
The code overflowed but I caught the exception.

The unchecked statement

A related keyword is unchecked. Type the following statement at the end of the previous statements. The compiler will not compile this statement because it knows it would overflow:

int x = int.MaxValue + 1;

Press F6 to build and notice the error, as shown in the following screenshot:

Note that this is a compile-time check. To disable compile-time checks, we can wrap the statement in an unchecked block, as shown in the following code:

unchecked
{
    int x = int.MaxValue + 1;
 WriteLine(x);
 x--;
 WriteLine(x);
 x--;
}

Press Ctrl + F5 and view the output in the console:

2147483646
2147483647
The code overflowed but I caught the exception.
-2147483648
2147483647
2147483646

Of course it would be pretty rare that you would want to explicitly switch off a check like this because it allows an overflow to occur. But, perhaps, you can think of a scenario where you might want that behavior.

主站蜘蛛池模板: 和平区| 格尔木市| 湖南省| 来凤县| 务川| 宝清县| 峨山| 阳城县| 怀安县| 肇州县| 沧州市| 永顺县| 禄劝| 济宁市| 乳源| 延长县| 宁乡县| 赞皇县| 灵石县| 广元市| 桐梓县| 武鸣县| 常山县| 安龙县| 乡宁县| 新乡县| 康定县| 汝南县| 印江| 桐城市| 望江县| 耒阳市| 大丰市| 峨眉山市| 天津市| 茂名市| 林州市| 济南市| 磴口县| 陆丰市| 乌恰县|