This program declares a checked programming context inside the Main method. Next, we assign an int variable to one greater than the maximum value that can be stored in an int.
class Program
{
static void Main()
{
checked
{
int value = int.MaxValue + int.Parse("1");
}
}
}Unhandled Exception: System.OverflowException:
Arithmetic operation resulted in an overflow.
With the checked context, bugs in our programs that would be silently ignored are changed into serious errors. Control flow is interrupted and programs are terminated.
Thus These bugs do not silently slip into the program. Using a checked context can help alert you to logic problems in your program faster.
Detail This could be trapped in a catch block if you needed to handle the problem at runtime.
Summary. The OverflowException helps us learn of logical errors. With the checked context, we detect these hard-to-find bugs. We improve the logic of our code.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 25, 2021 (rewrite).