PART I
C h a p t e r 1 3 :
E x c e p t i o n H a n d l i n g
361
PART IPART I
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) {
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
Do'stlaringiz bilan baham: |