Abnormal Handling Structured Abnormal Processing is a necessary link to the component design in modern distributed environments. The .NET universal language runs from the underlying structure to excenA residual support. In C #, the abnormal object is designed to encapsulate a class of various exception information (System.exception and its inheritance subclass, similar to the interface, it is recommended to name the suffix "Exception", but this is not necessary), " Try-catch-finally "statement and exception objects are designed for C # component to provide a package from exception detection, exception capture, and processing. We will might have an abnormal statement in the TRY sentence block, so that the exception appearing in the code may be detected and passed to the Catch statement. The CATCH statement can be used with parameters, or without parameters, the parameter type with the catch statement can only be system.exception and its inheritance subclass. The Catch statement with parameters will capture the exception of the parameter specified type and do not process for other exceptions. The Catch statement without parameters is equivalent to the exception of the parameter system.exception type (ie, all exceptions), but unusual variables cannot be obtained. A TRY selection can match multiple Catch statements to capture different exception types. Here, you must pay attention to the order of multiple CATCH statements, the parameter type in the later CATCH statement cannot be a subclass of the previous type, and cannot be the same as the previous parameter type. If the parameter type of the CATCH statement does not match the exception type detected by the TRY sentence, the exception will be thrown in the method of the statement. If the method is called without the corresponding detection and capture mechanism, the exception continues to be thrown until it is captured or thrown into the application's entry point main function, causing the program anomaly interrupt execution. This anomaly-thrown mechanism also applies to an exception that appears outside the TRY sentence - detects but not captured and no detection effect! Another point to note is that once the program has an exception, then the code behind it will not be executed! However, if the program logic needs to perform an exception or not, how to do some tasks urgently (typically close the open file, network port, etc.)? C # provides a Finally statement to solve this situation. Finally statement cannot be used alone, it and try, Catch have two matching sites: try, catch, finally all matching the try-catch-finally statement, in the alluating, even throwing an exception, Finally statement is also Execute; try the TRY statement to make a try-finally statement with the finally statement. After the try-finally statement detected an abnormality, it simply throws and did not capture. However, after this exception is processed (Catch statement capture to which it is called, or reaches the MAIN function entry point is executed), the finally statement is still executed. Of course, the finally statement block can only have one, which is not self-motivated.
The throw statement is used to throw an exception. In the code design, it encounters the conditions for violating the program design, or the abnormal situation, we often throw a specific exception, this time the throw statement is very useful, let's come See the following example: using System; public class MyException: ApplicationException {public MyException (String message): base (message) {} public MyException (String message, Exception inner): base (message, inner) {}} public class MyClass { public static void ExMethod () {throw new MyException ( "Exception in ExMethod");} public static void MyMethod () {try {ExMethod ();} catch (Exception e) {throw new MyException ( "Exception in MyMethod", e );}}} public class test {puric static void main () {mAClass.mymeth ();} catch (exception e) {console.writeline (); console.writeLine (); console.writeline (e. GetBaseException ());}}} program output: MyException: Exception in MyMethod -> MyException: Exception in ExMethod at MyClass.ExMethod () at MyClass.MyMethod () --End of inner exception stack trace-- at MyClass.MyMethod () at test.main () MyException: Exception in Exmethod at myclass.exmethod () at myclass.mymethod () on the above example, we have designed your own anomaly Myex Ception and this anomaly class is simply applied in its own design class. It shows its processing in the test program, and abnormal tracking in multiple method calls. Where E.GetBaseException () is a getBaseException () method that calls System.exception, obtaining the exception that causes exception E, if not, return E himself. Many methods and properties of System.Exception provide us with a good description and tracking service for exceptions. It is a good starting point for our application exception, exception, and understanding exception. Checked and Unchecked For "overflow exception" generated when the integer type is involved in arithmetic operation and type conversion, it is not true "exception" in some algorithms because of certain algorithms. Instead, this overflow is often used by the program. C # controls the need for this special situation by introducing CHECKED and UNCHECKED keywords. They can be added to a statement block (such as: checked {...}), or an arithmetic expression (such as unchecked (x y)), where the CHECKED flag is plus the statement or expression, if arithmetic overflow occurs, Then throw an exception of the System.OverflowException type, and the uncraft is not thrown when the unchecked flag is arithmetic overflow.