Learn to deal with anomalies
C # programmers can use a try block to partition code that might be affected by an exception. The associated catch block is used to handle any resulting exception. A finally block that contains code that runs regardless of try
whether an exception is thrown in the block (for example, freeing try
resources allocated in a block). A try
block requires one or more associated catch
blocks or a finally
block, or both.
A statement try-catch
, a statement try-finally
, and a statement are given here respectively try-catch-finally
.
Try-catch:
1 Static voidMain (string[] args)2 {3 Try4 {5 //code that needs to be executed6 }7 Catch(Exception e)8 {9 //Here you can get to the caught exceptionTen //you need to know how you should handle the exception One } A}
Try-finally:
1 Try 2 {3 // code 4 } required to execute 5 Finally6 {7 // code 8 executed after the try block }
Try-catch-finally:
1 Try2 {3 //code that needs to be executed4 }5 Catch(Exception e)6 {7 //Handling Exceptions here8 }9 finallyTen { One //code that executes after a try block (or possibly a catch block) A}
A block with a "memo" that does not come with catch
or finally
blocks try
will cause a compiler error.
catch block to catch exception
catch
The block can specify the type of exception to catch. The type specification is called an exception filter. The exception type should be derived from Exception. In general, Exception is not specified as an exception filter unless you know how to handle try
any exceptions that might be thrown in a block, or you catch
include a throw statement in a block.
Multiple blocks with different exception filters catch
can be concatenated together. Multiple catch
data blocks are evaluated in the code from the top to the bottom, but only one block of data is executed for each exception that is thrown catch
. The first block that best matches the specified exact type or its base class catch
is executed. If the catch
block does not specify a matching exception filter, the catch
block does not have the selected filter (if the statement has one). The block with the most specific (that is, the most derived) exception class needs to be catch
placed at the front.
You should catch an exception when the following conditions are true:
There is a specific understanding of why the exception was thrown, and a specific recovery can be implemented, for example, prompting the user to enter a new file name when capturing a FileNotFoundException object.
You can create a new, more specific exception and throw the exception.
1 DoubleGetnum (Double[] Nums,intindex)2 {3 Try4 {5 returnNums[index];6 }7 Catch(IndexOutOfRangeException e)8 {9 Throw NewArgumentOutOfRangeException ("Sorry, the index you want is out of bounds! ");Ten } One}
Hopefully, we'll usually handle some exceptions when passing exceptions. In the following example, the catch
block is used to add an error log before the exception is thrown again, which is a common practice.
1 Try2 {3 //try to access system resources4 }5 Catch(Exception e)6 {7 //Pseudo code: Log error Logs8 log. Error (e);9 Ten //and then re-throw the error One Throw; A}
release a Finally block of resources
You can use finally
blocks to clean up try
operations that are performed in a block. If present, the finally
block is executed at the end try
, after the block and any matching catch
blocks. Always runs regardless of whether an exception is thrown or if a block that matches the type of the exception is found catch
finally
.
You can use finally
blocks to free resources (such as file streams, database connections, and graphics handles) without waiting for the garbage collector in the runtime to complete the object. In fact, we recommend using a USE statement.
In the following example, a block is used to finally
close try
a file that is opened in a block. Note that you check the status of the file handle before closing the file. If the try
block cannot open the file, the file handle still has a value null
, and the finally
block does not attempt to close it. Or, if try
the file is successfully opened in a block, the finally
block closes the open file.
1 Static voidMain (string[] args)2 {3FileStream fs =NULL;4FileInfo fi =NewSystem.IO.FileInfo ("c:\\ The story of Xiao ER and small three. txt");5 6 Try7 {8FS =fi. OpenWrite ();9Fs. WriteByte (0);Ten } One finally A { - //Remember to judge null Oh, otherwise it may trigger other exceptions - if(FS! =NULL) the { - FS. Close (); - } - } + -}
"Bo Master" anti-bone Aberdeen
"Original" http://www.cnblogs.com/liqingwen/p/6193999.html
"Reference" Official Microsoft documentation
[C #] Learn to handle exceptions