[C #] learning to use exceptions,

Source: Internet
Author: User

[C #] learning to use exceptions,
Learn to use exceptions

In C #, runtime errors in the program are transmitted in the program by using a mechanism called "exception. An exception is caused by the error code and captured by the code that can correct the error. Exceptions can be caused by the. NET public Language Runtime (CLR) or by code in the program. Once an exception is thrown, the exception will be propagated in the call stack until it is foundcatchStatement. Uncaptured exceptions are handled by the general exception handler provided by the system, which is a dialog box that you often see.

Exceptions are represented by classes derived from exceptions. This class identifies the type of the exception and contains the attribute that describes the exception in detail. Here we will demonstrate how to customize a new exception class, and you can customize the attributes of configuration exceptions (optional), and then usethrowKeyword to raise this object (that is, an exception ).

1 /// <summary> 2 // define a new Exception 3 /// </summary> 4 class MyException: Exception 5 {6 public MyException (string msg) {} 7} 8 9 // <summary> 10 // throw the newly defined exception 11 /// </summary> 12 static void ThrowMyExcetion () 13 {14 throw new MyException ("Sorry, this is test! "); 15}

  

When an exception is thrown, check the current statement to check whether it is intryBlock. If yes, checktryAnycatchBlock to determine whether they can capture the exception.CatchThe block usually specifies the exception type.catchThe block type is the same (or matches) as the type of the base class of the exception or exception.catchBlock.

1 static void Main (string [] args) 2 {3 try 4 {5 ThrowMyExcetion (); // directly call Method 6} 7 catch (MyException e) 8 {9 Console. writeLine (e); 10} 11 12 Console. read (); 13}

1 static void Main (string [] args) 2 {3 StreamWriter sw = null; 4 5 try 6 {7 sw = new StreamWriter (@ "C: \ book \ story .txt "); 8 sw. write ("You are 250. "); 9} 10 catch (FileNotFoundException e) 11 {12 // place the exception in the first 13 Console. writeLine (e); 14} 15 catch (IOException e) 16 {17 // do not place it in the relative position 18 Console. writeLine (e); 19} 20 catch (Exception e) 21 {22 Console. writeLine (e); 23} 24 finally25 {26 if (sw! = Null) 27 {28 sw. Close (); 29} 30} 31 32 Console. Read (); 33}

 

RuncatchBefore the block, the runtime will checkfinallyBlock.FinallyBlocks enable programmers to clear abortedtryBlocks may be left in any fuzzy state, or any external resources (instance handle, database connection, or file stream) are released without waiting for the garbage collector to terminate these objects during runtime. For example:

1 static void Main (string [] args) 2 {3 FileStream fs = null; 4 FileInfo fi = new FileInfo (@ "story .txt "); 5 6 try 7 {8 fs = fi. openWrite (); 9 fs. writeByte (0); 10} 11 finally12 {13 // remember, if you forget to close, IO exceptions will occur! 14 // if (fs! = Null) 15 // {16 // fs. close (); 17 //} 18} 19 20 try21 {22 fs = fi. openWrite (); 23 fs. writeByte (1); 24 Console. writeLine ("OK! "); 25} 26 catch (IOException e) 27 {28 Console. WriteLine (" Fail! "); 29} 30 31 Console. Read (); 32}

[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.