lhmouse/asteria

`catch` operator?

Closed this issue · 2 comments

catch EXPRESSION catch(EXPRESSION) is equivalent to

(func() {
  try {
    (EXPRESSION);
    return null;
  }
  catch(e)
    return e;
}())

which is primarily useful in unit testing.

There is technical limitation, though:

In order for catch to catch exceptions that are effected by invalid expressions, its operand must be evaluated in a separate context. For example, the expression

catch(1/0)

is parsed (in Reverse Polish Notation) as

1 0 / catch

where the division operator effects an exception before the catch operator is encountered. For the catch operator to work expectedly, new syntax construction has to be set up.

This is resolved by requiring a pair of parenthesis around the operand.