Catch an OverflowException
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| C# Code Snippets |
| See also |
| edit |
This C# code snippet catches the System.OverflowException caused by numeric overflow—the condition of attempting to stuff a number into a field which is too small to represent that number.
short shorty = 32767; // largest positive short int integer = shorty + 1; try { checked // force compiler to object { shorty = (short) integer; // try to cram it into a short } } catch (OverflowException e) { //"Arithmetic operation resulted in an overflow." Console.WriteLine (e.Message); }