ECMAScript® 2024 Language Specification

Draft ECMA-262 / February 15, 2024

14.15 The try Statement

Syntax

TryStatement[Yield, Await, Return] : try Block[?Yield, ?Await, ?Return] Catch[?Yield, ?Await, ?Return] try Block[?Yield, ?Await, ?Return] Finally[?Yield, ?Await, ?Return] try Block[?Yield, ?Await, ?Return] Catch[?Yield, ?Await, ?Return] Finally[?Yield, ?Await, ?Return] Catch[Yield, Await, Return] : catch ( CatchParameter[?Yield, ?Await] ) Block[?Yield, ?Await, ?Return] catch Block[?Yield, ?Await, ?Return] Finally[Yield, Await, Return] : finally Block[?Yield, ?Await, ?Return] CatchParameter[Yield, Await] : BindingIdentifier[?Yield, ?Await] BindingPattern[?Yield, ?Await] Note

The try statement encloses a block of code in which an exceptional condition can occur, such as a runtime error or a throw statement. The catch clause provides the exception-handling code. When a catch clause catches an exception, its CatchParameter is bound to that exception.

14.15.1 Static Semantics: Early Errors

Catch : catch ( CatchParameter ) Block Note

An alternative static semantics for this production is given in B.3.4.

14.15.2 Runtime Semantics: CatchClauseEvaluation

The syntax-directed operation CatchClauseEvaluation takes argument thrownValue (an ECMAScript language value) and returns either a normal completion containing an ECMAScript language value or an abrupt completion. It is defined piecewise over the following productions:

Catch : catch ( CatchParameter ) Block
  1. Let oldEnv be the running execution context's LexicalEnvironment.
  2. Let catchEnv be NewDeclarativeEnvironment(oldEnv).
  3. For each element argName of the BoundNames of CatchParameter, do
    1. Perform ! catchEnv.CreateMutableBinding(argName, false).
  4. Set the running execution context's LexicalEnvironment to catchEnv.
  5. Let status be Completion(BindingInitialization of CatchParameter with arguments thrownValue and catchEnv).
  6. If status is an abrupt completion, then
    1. Set the running execution context's LexicalEnvironment to oldEnv.
    2. Return ? status.
  7. Let B be Completion(Evaluation of Block).
  8. Set the running execution context's LexicalEnvironment to oldEnv.
  9. Return ? B.
Catch : catch Block
  1. Return ? Evaluation of Block.
Note

No matter how control leaves the Block the LexicalEnvironment is always restored to its former state.

14.15.3 Runtime Semantics: Evaluation

TryStatement : try Block Catch
  1. Let B be Completion(Evaluation of Block).
  2. If B is a throw completion, let C be Completion(CatchClauseEvaluation of Catch with argument B.[[Value]]).
  3. Else, let C be B.
  4. Return ? UpdateEmpty(C, undefined).
TryStatement : try Block Finally
  1. Let B be Completion(Evaluation of Block).
  2. Let F be Completion(Evaluation of Finally).
  3. If F is a normal completion, set F to B.
  4. Return ? UpdateEmpty(F, undefined).
TryStatement : try Block Catch Finally
  1. Let B be Completion(Evaluation of Block).
  2. If B is a throw completion, let C be Completion(CatchClauseEvaluation of Catch with argument B.[[Value]]).
  3. Else, let C be B.
  4. Let F be Completion(Evaluation of Finally).
  5. If F is a normal completion, set F to C.
  6. Return ? UpdateEmpty(F, undefined).