try/catch/finally

try
catch
finally

1. Enclose the code that foresees an exception may be thrown in the try statement block.
2. If an exception occurs, transfer to the execution of the code in the catch statement block. There are three ways to write catch:
        a) catch
            This will catch any exception that occurs
        b) catch(Exception e)
            This will catch any exception that occurs, and provide the e parameter, you can use the e parameter to get the exception information when handling the exception
        c) catch( Exception's derived class e)
            This will catch the derived class's exception, for example, I want to catch an invalid operation exception, which can be written as follows:
            catch(InvalidOperationException e) {
                ....
            }
        This way, if the exception thrown in the try statement block It is an InvalidOperationException, which will be transferred to this place for execution, and other exceptions will not be handled.

        There can be multiple or no catches, and each catch can handle a specific exception. .net looks for exception handling blocks in the order of your catch, if found, it will be processed, if not found, it will be thrown to the upper level. If there is no upper level, it will be thrown to the user. At this time, if you are debugging, the program will stop running, and if it is a deployed program, it will abort.    

        Without the catch block, the exception is always thrown to the upper layer (if any), or interrupts the program.
3.
    Finally, there can be no or only one. It always runs at the end of this exception-handling structure, whether or not an exception occurred. Even if you return with return inside a try block, the finally always executes before returning, which gives you a chance to do some cleanup at the end of the exception handling. Such as closing the database connection and so on.
    Note: The finally block is required if there is no catch block.

    If you do not want to handle exceptions here, and submit them to the upper layer for processing when an exception occurs, but in this place, no matter whether an exception occurs or not, you must perform some operations, you can use try finally,
    a typical application is to perform database operations :
    Use the following primitive to illustrate:
try
{
    DataConnection.Open(); DataCommand.ExecuteReader     (
    );
    ...
    return;
}
finally
{
    DataConnection.Close(); Return, the finally block will always be executed, so that you have the opportunity to call Close to close the database connection (even if it is not opened or fails to open, the close operation can always be performed), so as to release the connection that has been created and free resources.



    By the way, return can be placed in a try block. But no matter when it returns, before returning, finally will execute.

Summary
    try { //The code executed, which may have exceptions. Once an exception is found, it immediately jumps to catch execution. Otherwise, the content in catch will not be executed}
    catch { //Unless an exception occurs in the execution code in try, the code here will not be executed}
    finally { //It will be executed no matter what, including the use of return in try catch, understandable As long as try or catch is executed, finally will be executed }     

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327037441&siteId=291194637