Execution steps:

  • try:
    1. Normally executed statements output 0
    2. If it encounters a throw statement, it goes to a catch block
  • catch:
    1. Execute the output statement in catch to print 1
    2. A catch statement ends when a return statement is encountered
    3. Execute the statement inside finally before actually returning
  • finally:
    1. The output of 3
    2. If a return statement is encountered, override the return in catch and return false

Detailed explanation:

  1. The purpose of a throw statement is to manually interrupt program execution and throw an error
  2. try… catch
    1. Allows error handling.
    2. The try block throws an error, and the JavaScript engine immediately transfers the execution of the code to the catch block, or the error is caught by the catch block.
    3. After the catch block catches an error, the program does not break and continues to execute as normal.

  1. finally
    1. Represents a statement that must be run last, regardless of whether an error occurs
    2. The following example shows that a return statement is executed before finally code and only returns when finally code is finished.

  1. In a catch block, the flag that triggers the passing ina finally block includes both a return statement and a throw statement.

When you enter a catch block, as soon as you encounter a throw, you execute a finally block that contains a return false statement, so you return directly and do not go back to the rest of the catch block.