Preface
A common method used by catch and finally is to obtain and use resources in the try block, handle exceptions in the Catch Block, and release resources in the Finally block.
Catch
Catch this will catch any exceptions.
Catch (exception e) This will capture any exceptions. In addition, the E parameter is also provided. You can use the e parameter to obtain information about the exception when handling the exception.
Catch (exception derived class E) This will capture exceptions defined by the derived class. For example, if I want to catch an exception with an invalid operation, write catch (invalidoperationexception e) as follows) {....} in this way, if the exception thrown in the try statement block is invalidoperationexception, it will be transferred to this place for execution. Other exceptions will not be processed.
Catch can have multiple or none. Each catch can handle a specific exception .. . Net searches for exception handling blocks in the catch order. If the block is found, the block is processed. If the block is not found, the block is thrown to the previous level. If there is no previous level, it will be thrown to the user. At this time, if you are debugging,ProgramThe running will be interrupted. If it is a deployed program, it will be aborted.
If no Catch Block exists, an exception is always thrown up the layer (if any) or the program is interrupted.
Finally
Finally can be none or only one. Whether an exception occurs or not, it always runs at the end of the exception handling structure. Even if you use return in the try block, finally always needs to be executed before the return, so that you have the opportunity to do some cleanup work at the end of exception handling. For example, close the database connection. NOTE: If there is no catch block, the Finally block is required.
If you do not want to handle exceptions here, but submit them to the upper layer for processing when exceptions occur, you must perform some operations in this place no matter what exceptions occur, try finally can be used. A typical application is to perform database operations: Use the following primitive to describe it:
Try{Dataconnection. open (); datacommand. executereader ();...Return;}Finally{Dataconnection. Close ();}
No matter whether an exception is thrown or where return is returned, the finally statement block is always executed, so you have the opportunity to call close to close the database connection (even if it is not opened or fails to be opened, close operations can always be executed) to release existing connections and resources.
Return
Next let's look at the first small example.
Using System; Public Class Test1 { Public Static Void Main () {console. writeline (test (); console. Readline ();} Public Static Int Test (){ Int I = 1 ; Try { Return I ;} Finally {I ++ ;}}}
The printed result is 1.
Example 2
Using System; Public Class Class1 { Public Int Value1 =1 ;} Public Class Test1 { Public Static Void Main () {console. writeline (test (). value1); console. Readline ();} Public Static Class1 test () {class1 C = New Class1 (); Try { Return C ;} Finally {C. value1 ++ ;}}}
The result is 2.
to explain this difference, you need to look at its Il and understand it from the perspective of calling functions and parameter stacks. CLR also has stacks during execution, but the stack is used differently from the stack in the traditional local Code . The stack in local code is very useful. It can be used not only to temporarily Save the register value, but also to save local variables. In addition, it can be used to save some or all parameters passed to the function, function return values are generally transmitted through the eax register, rather than the stack. However, in CLR, local variables are not explicitly stored using stacks, while stacks are only used to pass parameters when calling functions. In addition, function return values are also stored using stacks. When a function is called, the parameters required by the function are pushed to the stack in sequence, and these parameters are directly used in the function. When the function returns, the return value is pushed to the stack. After the function returns, the stack top is the return value. If the caller does not care about the return value, execute the pop statement to bring up the return value. This ensures that the function is at the same position on the top of the stack before and after the call.
when passing parameters through the pressure stack, the parameter types are different, and the pressure stack content is also different. If it is a value type, the pressure stack is the copied parameter value, if it is a reference type, then the stack is only a reference, which is what we are familiar, when the value type is passed, modifying the parameter value in the function does not affect the function, but the referenced type does.
when we execute new in the Code, the corresponding Il is newobj. The result is to create a testclass2 object and return a reference and place it on the stack, the subsequent stloc will save this reference as a local Variable, so there is no other content on the stack. The try block does not perform too many operations. It just places the saved reference on the stack and saves it as another local variable. This local variable is the reference to be returned later, at this time, we have two local variables, but they point to two references of the same object. The finally block is first taken out of the reference saved at the beginning and placed on the stack. The DUP statement adds a identical reference at the top of the stack, then the ldquo statement is to take a member from the top object of the stack and place it on the stack. The obtained member is value, and then press 1 to the stack, and then execute add, the process of 1 + 1 = 2 is realized. Add pops up two values from the stack, and then presses a value back to the stack. Then, call stfld to set 2 of the stack to the value attribute of the referenced object under 2 of the stack. The finally part is the true return, which tries to extract the second local variable we saved and press the stack to use it as the return value. But for the reference type, it refers to the same object as the reference of the previous operation. Therefore, the operation in the Finally block will affect the return value, which is very easy to understand.