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

With parameter thrownValue.

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 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 Completion(status).
  7. Let B be the result of evaluating Block.
  8. Set the running execution context's LexicalEnvironment to oldEnv.
  9. Return Completion(B).
Catch : catch Block
  1. Return the result of evaluating 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 the result of evaluating Block.
  2. If B.[[Type]] is throw, let C be CatchClauseEvaluation of Catch with argument B.[[Value]].
  3. Else, let C be B.
  4. Return Completion(UpdateEmpty(C, undefined)).
TryStatement : try Block Finally
  1. Let B be the result of evaluating Block.
  2. Let F be the result of evaluating Finally.
  3. If F.[[Type]] is normal, set F to B.
  4. Return Completion(UpdateEmpty(F, undefined)).
TryStatement : try Block Catch Finally
  1. Let B be the result of evaluating Block.
  2. If B.[[Type]] is throw, let C be CatchClauseEvaluation of Catch with argument B.[[Value]].
  3. Else, let C be B.
  4. Let F be the result of evaluating Finally.
  5. If F.[[Type]] is normal, set F to C.
  6. Return Completion(UpdateEmpty(F, undefined)).