C # basic syntax Learning (10 ),
C # overflow may occur when data is displayed and converted. In this case, you can use the keyword checked to check for overflow:
Checked (<expression>) checks for Overflow
Unchecked (<expression>) does not check Overflow
If you use checked to check for overflow, System will be thrown once the overflow occurs. overflowException. You can also configure the IDE to enable the overflow check by default. Unless unchecked is added, an exception will be thrown once overflow occurs. The default overflow check for configuration projects is as follows:
An example of using checked is as follows:
1 static void Main (string [] args) 2 {3 Int32 a = 123456; 4 Int16 B = 0; 5 6 B = checked (Int16) a); 7 8 Console. writeLine ("a = {0} \ r \ nb = {1}", a, B); 9 10 Console. writeLine ("Press any key to exit! "); 11 Console. ReadKey (); 12}
Running result:
Unhandled Exception: System. overflowException: Arithmetic operation resulted inan overflow. at CheckOverflow. program. main (String [] args) in d: \ Nick \ code \ C # \ test \ CheckOverflow \ Program. cs: line 16
An exception is thrown during conversion overflow.