Skip to content
Advertisement

What was a GuardedCatchClause in the SpiderMonkey parser?

The SpiderMonkey JavaScript engine’s parser once supported a nonterminal symbol GuardedCatchClause, which when parsed would produce entries in the .guardedHandlers property of the associated TryStatement; this property also appeared in the ESTree spec at the time.

Unfortunately I have not found any references to the syntax of try statements with GuardedCatchClauses on the web. What was the actual syntax and semantics? Were these essentially catch clauses with an additional if-like conditional?

Additionally: was GuardedCatchClause part of the ES4 proposals? (I have never seen reference to this in any of the ES4 feature retrospectives I have read.)

Advertisement

Answer

It does refer to conditional catch clauses. You can find a description of them in the web archive of the Mozilla documentation:

Conditional catch clauses

Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

You can also use one or more conditional catch clauses to handle specific exceptions. In this case, the appropriate catch clause is entered when the specified exception is thrown. In the following example, code in the try block can potentially throw three exceptions: TypeError, RangeError, and EvalError. When an exception occurs, control transfers to the appropriate catch clause. If the exception is not one of the specified exceptions and an unconditional catch clause is found, control transfers to that catch clause.

If you use an unconditional catch clause with one or more conditional catch clauses, the unconditional catch clause must be specified last. Otherwise, the unconditional catch clause will intercept all types of exception before they can reach the conditional ones.

Reminder: this functionality is not part of the ECMAScript specification and has been removed in Firefox 59. It’s not supported in any current browser anymore.

try {
  myroutine(); // may throw three types of exceptions
} catch (e if e instanceof TypeError) {
  // statements to handle TypeError exceptions
} catch (e if e instanceof RangeError) {
  // statements to handle RangeError exceptions
} catch (e if e instanceof EvalError) {
  // statements to handle EvalError exceptions
} catch (e) {
  // statements to handle any unspecified exceptions
  logMyErrors(e); // pass exception object to error handler
}

Here is the same “Conditional catch clauses” using code that conforms to ECMAScript specification (obviously it’s verbose, but works everywhere):

try {
  myroutine(); // may throw three types of exceptions
} catch (e) {
  if (e instanceof TypeError) {
    // statements to handle TypeError exceptions
  } else if (e instanceof RangeError) {
    // statements to handle RangeError exceptions
  } else if (e instanceof EvalError) {
    // statements to handle EvalError exceptions
  } else {
    // statements to handle any unspecified exceptions
    logMyErrors(e); // pass exception object to error handler
  }
}

As noted, they were removed from Firefox in version 59 (March 13, 2018)

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement