Checked and UnChecked Exceptions in C# -Learn about how an exception occur?, What kind of Exception occurs at compile time and Runtime of code flow. Also Learn in Which situation Checked exception and Unchecked exception is required.
- Exception is an error that occurs at compile time or run time.
- Compile time error occurs at compile time due to syntax errors
- Runtime error that occurs during the execution of Program. This errors are because of improper logics in the code.
- .NET Runtime environment terminates the program execution when an exception occurs.
For example,
int val1=Int32.MaxValue;
int val2=Int32.MaxValue;
int val3=val1+val2;
If we try to sum the values of val1 and val2 and store it into val3 variable, then it stores a garbage value because the maximum limit storing value of val3 is reached. val3 loss the data.
C# program for adding maximum int values
C# code
class Sample
{
static void Main(string[] args)
{
//storing max value of int datatype into val1 variable
int val1 = Int32.MaxValue;
//storing max value of int datatype into val1 variable
int val2 = Int32.MaxValue;
int val3 = val1 + val2;
Console.WriteLine("val1 = {0}",val1);
Console.WriteLine("val2 = {0}", val2);
Console.WriteLine("sum of val1 and val2 = {0}", val3);
}
}
Output
val1 = 2147483647
val2 = 2147483647
sum of val1 and val2 = -2
If we observe the above code and output, the val1 variable storing 2147483647 and val2 variable storing 2147483647.
The result of val1+val2 is -2 which is improper. This is because of value overflow of maximum Int32 value.
To avoid the improper results we can check the maximum limit value of any arithmetic operation using Checked Exceptions.
C# program to handle the code to avoid overflow of value.
C# Code
class Sample
{
static void Main(string[] args)
{
//storing max value of int datatype into val1 variable
int val1 = Int32.MaxValue;
//storing max value of int datatype into val1 variable
int val2 = Int32.MaxValue;
//checked keyword raises an exception if overflow occurs when performing arithmetic operation.
int val3 = checked(val1 + val2);
Console.WriteLine("val1 = {0}",val1);
Console.WriteLine("val2 = {0}", val2);
Console.WriteLine("sum of val1 and val2 = {0}", val3);
}
}
If we try to run the above code you will get a Runtime Exception stating that “overflow Exeption was un handled” and the program terminates its execution. This is because of checked keyword in the above program.
Checked Exception
The Checked keyword is used to control the overflow checking of arithmetic operations and value conversions from one data type to another data type.
For Example Int32 value to Int16 conversion.
Checked keyword forces the compiler to check the unexpected overflow results in the program.
Syntax for Checked statement
variable=Checked(expression);
Syntax for Checked block
checked
{
}
C# Program to handle the checked Exception using try and Catch blocks.
Below program is with Checked statement
C# code
class Sample
{
static void Main(string[] args)
{
//storing max value of int datatype into val1 variable
int val1 = Int32.MaxValue;
//storing max value of int datatype into val1 variable
int val2 = Int32.MaxValue;
try
{
//checked statement.
int val3 = checked(val1 + val2);
Console.WriteLine("val1 = {0}", val1);
Console.WriteLine("val2 = {0}", val2);
Console.WriteLine("sum of val1 and val2 = {0}", val3);
}
catch(OverflowException e)
{
Console.WriteLine(e.ToString());
}
}
}
Output
System.OverflowException: Arithmetic operation resulted in an overflow.
at Checked_and_Unchecked_Exceptions.Sample.Main(String[] args) in e:\PRACTICE
CODEFORALLDOTNETBASICS\MASTERPROJECT\Checked and Unchecked Exceptions\Checked an
d Unchecked Exceptions\Program.cs:line 21
below program is with Checked block.
C# code
class Sample
{
static void Main(string[] args)
{
//storing max value of int datatype into val1 variable
int val1 = Int32.MaxValue;
//storing max value of int datatype into val1 variable
int val2 = Int32.MaxValue;
int val3 ;
try
{
//checked block.
checked
{
val3 = val1 * val2;
}
Console.WriteLine("val1 = {0}", val1);
Console.WriteLine("val2 = {0}", val2);
Console.WriteLine("sum of val1 and val2 = {0}", val3);
}
catch(OverflowException e)
{
Console.WriteLine(e.ToString());
}
}
}
Output
System.OverflowException: Arithmetic operation resulted in an overflow. at Checked_and_Unchecked_Exceptions.Sample.Main(String[] args) in e:\PRACTICE CODEFORALLDOTNETBASICS\MASTERPROJECT\Checked and Unchecked Exceptions\Checked an d Unchecked Exceptions\Program.cs:line 25
Unchecked Exception
Unchecked keyword is used to give garbage value when an overflow occurs while performing arithmetic operations or value conversions from one data type to another data type.
This is a default behaviour in C#.
Unchecked keyword force the compiler to ignore the unexpected overflow results or data type conversions in the program.
C# Program using unchecked keyword
below is the program using unchecked statement.
c#code
class Sample
{
static void Main(string[] args)
{
//storing max value of int datatype into val1 variable
int val1 = Int32.MaxValue;
//storing max value of int datatype into val1 variable
int val2 = Int32.MaxValue;
//un checked statement.
int val3 = unchecked(val1 * val2);
Console.WriteLine("val1 = {0}", val1);
Console.WriteLine("val2 = {0}", val2);
Console.WriteLine("sum of val1 and val2 = {0}", val3);
}
}
output
val1 = 2147483647
val2 = 2147483647
sum of val1 and val2 = 1
below is the program using unchecked block.
C# code
class Sample
{
static void Main(string[] args)
{
//storing max value of int datatype into val1 variable
int val1 = Int32.MaxValue;
//storing max value of int datatype into val1 variable
int val2 = Int32.MaxValue;
int val3 ;
//un checked block.
unchecked
{
val3 = val1 * val2;
}
Console.WriteLine("val1 = {0}", val1);
Console.WriteLine("val2 = {0}", val2);
Console.WriteLine("sum of val1 and val2 = {0}", val3);
}
}
Output
val1 = 2147483647
val2 = 2147483647
sum of val1 and val2 = 1