exception mechanism

Handling exceptions:

  1、try catch 

    1.1 The following statement in the try block where an exception occurs will not be executed, the matching catch block code will execute normally, and the rest of the thread code will execute normally.

  2、try finally

    2.1 The statement below the statement where an exception occurs in the try block will not be executed, the finally block statement will be executed, and the rest of the thread code will not be executed.

  3、try catch finally

    3.1 The following statement in the try block where an exception occurs will not be executed, the matching catch block code will be executed normally, the finally block statement will be executed, and the rest of the thread code will be executed normally.

Use points:

  1 The exception is caught and the thread continues execution.

  2 Regardless of the try block or the catch block, finally will be executed.

  3. It is best to wrap the timed task with try catch to prevent the timed task from being terminated due to an exception.

  4 Display the lock (Lock) Use try to wrap the code block that needs to be locked, and be sure to release the lock in finally, so as to prevent the deadlock phenomenon caused by the inability to release the lock when the program is abnormal.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325233962&siteId=291194637