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 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, the program will be interrupted, and 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 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:
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.
Next let's look at the first small example.
i=++
The printed result is 1.
Example 2
Value1= =++
The result is 2.