[C #] C # knowledge Review,
Exception description
C # The exception handling function of the language helps you handle exceptions or exceptions when the program is running. Exception Handlingtry
,catch
Andfinally
And other keywords to try to execute some operations to handle failures. Of course, although these operations may also fail, if you are sure you want to do so and want to clear resources afterwards, you can try this. Common Language Runtime (CLR),. NET, or any third-party library or application code may generate exceptions. Exception: You can usethrow
Keyword is explicitly created.
In many cases, exceptions may not be caused by methods directly called by code, but by another method with a lower position in the call stack. In this case, CLR expands the stack to find whether there are methods that containcatch
Block. If this method is found, the first such method will be executed.catch
Block. If no appropriate location is found in the call stackcatch
The process is terminated and an error message is displayed to the user.
See the example. Here we use a method to check whether there is a division by zero. If so, this exception is caught. If no exception is handled, the program terminates and generates the error "DivideByZeroException not handled.
1 /// <summary> 2 /// Division 3 /// </summary> 4 /// <param name = "x"> </param> 5 /// <param name = "y"> </param> 6 // <returns> </returns> 7 static double Division (double x, double y) 8 {9 if (y = 0) 10 {11 throw new DivideByZeroException (); 12} 13 14 return x/y; 15} 16 17 static void Main (string [] args) 18 {19 // suppose 20 double x = 250, y = 0; 21 22 try23 {24 var result = Division (x, y); 25 Console. writeLine ($ "result: {result}"); 26} 27 catch (DivideByZeroException e) 28 {29 30 Console. writeLine (e); 31} 32 33 Console. read (); 34}
C # basic Review Series
C # knowledge Review-serialization
C # knowledge Review-Expression Tree Expression Trees
C # knowledge Review-feature Attribute and AssemblyInfo. cs-understanding common feature attributes
C # knowledge Review-delegated delegate and C # knowledge Review-delegated delegate (continued)
C # knowledge Review-Introduction to events and C # knowledge Review-Event Events
There is no unspeakable secret between the string, the String, the big S, and the small S
C # knowledge Review-packing and unpacking
[Blogger] Anti-Bot
[Source] http://www.cnblogs.com/liqingwen/p/6193354.html
[Reference] Microsoft official documentation