today we are studying the exception handling mechanism in C #.
First, exception handling is used to prevent errors that can be predicted but not completely prevented. Using the proper exception handling mechanism can effectively prevent the program from crashing. In this sense, some of the other bugs in the program, such as bugs, errors need to use the application to handle user errors.
Exception handling has the following fields: Try,throw,catch. Use exception handling to use the System.Exception namespace. The thrown exception will continue along the inheritance chain of the class until it is passed to System.Exception (this is the same mechanism as C + + and Java).
1 Public classTest2 {3 Public Static voidMain ()4 {5Console.WriteLine ("Enter Main ...");6Test T =NewTest ();7 t.func1 ();8Console.WriteLine ("Exit Main ...");9 }Ten Public voidFunc1 () One { AConsole.WriteLine ("Enter Func1 ..."); - Func2 (); -Console.WriteLine ("Exit Func1 ..."); the } - - Public voidFunc2 () - { +Console.WriteLine ("Enter Func2 ..."); - Throw Newsystem.applicationexception (); +Console.WriteLine ("Exit Func2 ..."); A } at } -}
Exception Handling in C #