As a developer, you sometimes seem to spend more time checking for errors and handling them than you do on the core logic of the actual program. You can address this issue by using exceptions. Exceptions are designed to handle errors, my intention is not cover the whole exception topic, but just a specific C# feature which allow us to handle one of the most common exception.

By default, a C# program will not check arithmetic for overflow. The following code provides an example:
// example.cs
class Example
{
static void Main( )
{
int number = int.MaxValue;
Console.WriteLine(++number);
}
}
In the preceding code, number is initialized to the maximum value for an int. The expression ++number increments number to –2147483648, the largest negative int value, which is then written to the console. No error message is generated.
Controlling Arithmetic Overflow Checking
When compiling a C# program, you can globally turn on arithmetic overflow checking by using the /checked+ command line option, as follows:
c:\ csc /checked+ example.cs
The resulting executable program will cause an exception of class System.OverflowException. Similarly, you can turn off global arithmetic overflow checking by using the /checked- command line option, as follows:
c:\ csc /checked- example.cs
The resulting executable program will silently wrap the int value to int.MinValue and will not cause an exception of class System.OverflowException.
Creating Checked and Unchecked Statements
You can use the checked and unchecked keywords to create statements that are explicitly checked or unchecked statements:
checked { statement-list }
unchecked { statement-list }
Regardless of the compile-time /checked setting, the statements inside a checked statement list are always checked for arithmetic overflow. Similarly, regardless of the compile-time /checked setting, the statements inside an unchecked statement list are never checked for arithmetic overflow.
Creating Checked and Unchecked Expressions
You can also use the checked and unchecked keywords to create checked and unchecked expressions:
checked ( expression )
unchecked ( expression )
A checked expression is checked for arithmetic overflow; an unchecked expression is not. For example, the following code will generate a System.OverflowException.
// example.cs
class Example
{
static void Main( )
{
int number = int.MaxValue;
Console.WriteLine(checked(++number));
}
}