[C #] C # knowledge Review,

Source: Internet
Author: User

[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,catchAndfinallyAnd 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 usethrowKeyword 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 containcatchBlock. If this method is found, the first such method will be executed.catchBlock. If no appropriate location is found in the call stackcatchThe 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.