Home > Article > Backend Development > C# checked
To explicitly check the overflow for integral type operations and conversions in arithmetic, the overflow checking must be enabled for integral type operations and conversions in arithmetic and this is possible by making use of a keyword called checked keyword in C# and by using checked keyword for expression in C#, the constant expression is checked for overflow during run time and if there is overflow, overflow exceptions are raised by the compiler whereas the expressions those are non-constant are not checked for overflow during the run time despite the use of checked keyword and overflow, exceptions are not raised in such cases.
The syntax of checked keyword C# is as follows:
checked(Expression)
Code:
//a namespace called check is defined namespace check { //a class called space is defined public class space { //main method is called public static void Main() { // Using checked keyword to check if the value is over flowing checked { // Increasing the value upto the value of maximum int number = 0; for (int x = 0; x < int.MaxValue; x++) { number++; } // Increasing the value upto the value of maximum again (error). for (int x = 0; x < int.MaxValue; x++) { number++; } } } } }
Output:
In the above program, a namespace called check is defined. Then a class called space is defined. Then the main method is called. Then we have used checked keyword to check if the value is overflowing by increasing the value up to the value of maximum. If we do not make use of the checked keyword in the above program, no exception will be thrown. Because we have made use of checked keyword in our program, an exception as shown in the output snapshot is thrown. The checked keyword in the program can be activated by selecting the option in the compiler “/checked”. An overflow exception is raised when the variable number is incremented by one every time to reach the maximum value of the integer which cannot be fit in the memory location. Overflow exception is raised during the run time by using special intermediate instructions.
Here are the following examples mention below
C# program to demonstrate the use of a checked keyword to throw exceptions.
Code:
using System; //a namespace called check is defined namespace check { //a class called space is defined public class space { //main method is called public static void Main() { // Using checked keyword to check if the value is over flowing checked { int twenty = 20; int j = 2147483647 + twenty; Console.WriteLine(j); } } } }
Output:
In the above program, a namespace called check is defined. Then a class called space is defined. Then the main method is called. Then we have used checked keyword to check if the value is overflowing by assigning the value 20 to a variable twenty and then adding it to the maximum value of the integer so that an exception will be thrown. If we do not make use of the checked keyword in the above program, no exception will be thrown. Because we have made use of checked keyword in our program, an exception as shown in the output snapshot is thrown. The checked keyword in the program can be activated by selecting the option in the compiler “/checked”. An overflow exception is raised when the value of the variable twenty is added to the maximum value of the integer which cannot be fit in the memory location. Overflow exception is raised during the run time by using special intermediate instructions.
C# program to demonstrate the use of a checked keyword to throw exceptions.
Code:
using System; //a class called check is defined public class Check { //the maximum value of integer is assigned to a variable static int maxInt = 2147483647; //main method is called public static void Main() { Console.WriteLine(CheckMethod()); } static int CheckMethod() { int y = 0; try { y = checked (maxInt + 1); } catch (System.OverflowException e) { Console.WriteLine("An overflow exception is raised because of maximum integer value " + e.ToString()); } return y; } }
Output:
In the above program, a class called Check is defined. Then the maximum value of the integer is assigned to a variable maxInt. Then the main method is called. Then we call the check method function within which we have made use of the try-catch block which consists of checked keyword usage to add one to the maximum value of the integer and the catch block consists of the exception statement we want to be displayed. If we do not make use of the checked keyword in the above program, no exception will be thrown. Because we have made use of checked keyword in our program, an exception as shown in the output snapshot is thrown. The checked keyword in the program can be activated by selecting the option in the compiler “/checked”. An overflow exception is raised when the value of the variable twenty is added to the maximum value of the integer which cannot be fit in the memory location. Overflow exception is raised during the run time by using special intermediate instructions.
In this tutorial, we understand the concept of the checked keyword in C# through definition, the syntax of the checked keyword in C#, Working of the checked keyword in C# through examples, and their outputs.
The above is the detailed content of C# checked. For more information, please follow other related articles on the PHP Chinese website!