[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 foundcatch
Statement. 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 usethrow
Keyword 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 intry
Block. If yes, checktry
Anycatch
Block to determine whether they can capture the exception.Catch
The block usually specifies the exception type.catch
The block type is the same (or matches) as the type of the base class of the exception or exception.catch
Block.
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}
[Reference] Microsoft official documentation