checked and unchecked Statements

C# has built-in expressions for checking the overflow context of arithmetic operations and conversions. These are checked and unchecked statements. checked statements watch expressions for evidence of overflow. When overflow occurs, the system raises an exception. Listing 10.7 shows how the checked statement causes an OverflowException to be generated.'

Listing 10.7. checked Statements: checked.cs
 using System; public class ExceptionTester { public static int Main(string[] args) { int prior = 250000000; int after = 150000000; int total; try { checked { total = prior * after; } } catch (OverflowException oe) { Console.WriteLine("\nOverflow Message: {0}", oe.Message); } catch (Exception e) { Console.WriteLine("\nMessage: ...

Get C# Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.