Using checked and unchecked


A special feature in C# relates to the generation of overflow exceptions in arithmetic computations. As you know, it is possible for some types of arithmetic computations to produce a result that exceeds the range of the data type involved in the computation. When this occurs, the result is said to overflow. For example, consider the following sequence:

 byte a, b, result; a = 127; b = 127; result = (byte)(a * b);

Here, the product of a and b exceeds the range of a byte value. Thus, the result overflows the type of the result.

C# allows you to specify whether your code will raise an exception when overflow occurs by using the keywords checked and unchecked. To specify that an expression be checked for overflow, use checked. To specify that overflow be ignored, use unchecked. In this case, the result is truncated to fit into the target type of the expression.

The checked keyword has these two general forms. One checks a specific expression and is called the operator form of checked. The other checks a block of statements.

 checked (expr) checked {    // statements to be checked }

Here, expr is the expression being checked. If a checked expression overflows, then an OverflowException is thrown.

The unchecked keyword has these two general forms. One is the operator form, which ignores overflow for a specific expression. The other ignores overflow for a block of statements.

 unchecked (expr) unchecked {    // statements for which overfl ow is ignored }

Here, expr is the expression that is not being checked for overflow. If an unchecked expression overflows, then truncation will occur.

Here is a program that demonstrates both checked and unchecked:

 // Using checked and unchecked. using System; class CheckedDemo {   public static void Main() {     byte a, b;     byte result;     a = 127;     b = 127;     try {       result = unchecked((byte)(a * b));       Console.WriteLine("Unchecked result: " + result);       result = checked((byte)(a * b)); // this causes exception       Console.WriteLine("Checked result: " + result); // won't execute     }     catch (OverflowException exc) {       // catch the exception       Console.WriteLine(exc);     }   } }

The output from the program is shown here:

 Unchecked result: 1 System.OverflowException: Arithmetic operation resulted in an overflow.    at CheckedDemo.Main()

As is evident, the unchecked expression resulted in a truncation. The checked expression caused an exception.

The preceding program demonstrated the use of checked and unchecked for a single expression. The following program shows how to check and uncheck a block of statements:

 // Using checked and unchecked with statement blocks. using System; class CheckedBlocks {   public static void Main() {     byte a, b;     byte result;     a = 127;     b = 127;     try {       unchecked {         a = 127;         b = 127;         result = unchecked((byte)(a * b));         Console.WriteLine("Unchecked result: " + result);         a = 125;         b = 5;         result = unchecked((byte)(a * b));         Console.WriteLine("Unchecked result: " + result);       }       checked {         a = 2;         b = 7;         result = checked((byte)(a * b)); // this is OK         Console.WriteLine("Checked result: " + result);         a = 127;         b = 127;         result = checked((byte)(a * b)); // this causes exception         Console.WriteLine("Checked result: " + result); // won't execute       }     }     catch (OverflowException exc) {       // catch the exception       Console.WriteLine(exc);     }   } }

The output from the program is shown here:

 Unchecked result: 1 Unchecked result: 113 Checked result: 14 System.OverflowException: Arithmetic operation resulted in an overflow.    at CheckedBlocks.Main()

As you can see, the unchecked block results in the overflow being truncated. When overflow occurs in the checked block, an exception is raised.

One reason that you may need to use checked or unchecked is that the default checked/unchecked status of overflow is determined by the setting of a compiler option and the execution environment, itself. Thus, for some types of programs, it is best to explicitly specify the overflow check status.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net