[C #] C # knowledge Review,
Learn to use exceptions
In C #, errors that occur during runtime in the program will be continuously transmitted in the program. This mechanism is called "exception ". An exception is usually caused by the error code and caught by the code that can correct the error. Exceptions can be caused by. net clr or code in the program. Once an exception is thrown, the exception will be continuously propagated in the call stack until the matchingcatch
Statement. Exceptions without catch are handled by the default exception handler provided by the system, which is a dialog box that suddenly causes debugging interruption and displays exception information.
All exceptions are derived from exceptions. The types of these exceptions include the attributes that describe the exceptions in detail. Here I will customize a new exception class. In fact, I can also customize the attribute of the configuration exception (this is optional). Then I usethrow
This object (that is, an exception) is thrown when the keyword is displayed ).
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, the program checks the current statement to determine whether it is included intry
Block. If yes, it will checktry
All associated Blockscatch
Block to determine whether they can catch the exception. The catch Block usually specifies the exception type.catch
If the block type is the same (or matches) as the exception or its base classcatch
Blocks can be captured and processed.
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}
Runcatch
Before the block, the runtime will checkfinally
Block.Finally
Blocks enable programmers to clear abortedtry
Blocks 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}
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-exception Introduction
[Blogger] Anti-Bot
[Source] http://www.cnblogs.com/liqingwen/p/6193534.html
[Reference] Microsoft official documentation