C# - Unchecked statement
In C#, when using the 'unchecked' statement, the program doesn't handle stack overflow exceptions, and it continues to run the code. This can lead to outputs that are not correct. For a clearer explanation, examine the code provided below:
private void button10_Click(object sender, EventArgs e)
{
uncheckedExample();
}
void uncheckedExample()
{
int val1 = 2147483647;
int val2 = 100;
try
{
unchecked
{
int val3 = val1 + val2;
Console.WriteLine(val3);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
The output shown is incorrect; however, you'll notice that no overflow exception occurs because the 'unchecked' statement is in use.
-1247483549