1. Exceptions may be foreseen Code Included in the try statement block.
2. If an exception occurs, it is transferred to the catch execution. Catch can be written in several ways:
Catch
This will capture 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 the exceptions defined by the derived class. For example, if you want to catch an exception with an invalid operation, you can write as follows:
Catch (invalidoperationexception E)
{
....
}
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.
3. 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,
A typical application is database operations:
Use the following primitive to describe:
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.
By the way, return can be placed in the try statement block. However, no matter when it is returned, finally will execute before it is returned.
Summary:
Try {// executed code, which may be abnormal. Once an exception is found, the system immediately jumps to catch for execution. Otherwise, the content in the catch will not be executed}
Catch {// The code here will not be executed unless an exception occurs during code execution in try}
finally {// It will be executed no matter what the situation is, including the use of return in try catch, which can be understood as long as a try or catch is executed, it will certainly execute finally}