Name: proposal-throw-expressions
Owner: Ecma TC39
Description: Proposal for ECMAScript 'throw' expressions
Created: 2017-07-15 18:24:20.0
Updated: 2018-05-23 19:56:29.0
Pushed: 2018-01-23 19:52:31.0
Homepage: https://tc39.github.io/proposal-throw-expressions
Size: 168
Language: HTML
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
throw
expressionsThis proposal defines new syntax to throw exceptions from within an expression context.
Stage: 2
Champion: Ron Buckton (@rbuckton)
For more information see the TC39 proposal process.
A throw
expression allows you to throw exceptions in expression contexts. For example:
tion save(filename = throw new TypeError("Argument required")) {
(ast, {
: () => throw new Error("avoid using 'with' statements.")
tion getEncoder(encoding) {
t encoder = encoding === "utf8" ? new UTF8Encoder()
: encoding === "utf16le" ? new UTF16Encoder(false)
: encoding === "utf16be" ? new UTF16Encoder(true)
: throw new Error("Unsupported encoding");
s Product {
id() { return this._id; }
id(value) { this._id = value || throw new Error("Invalid value"); }
A throw
expression does not replace a throw
statement due to the difference
in the precedence of their values. To maintain the precedence of the throw
statement,
we must add a lookahead restriction to ExpressionStatement
to avoid ambiguity.
yExpression[Yield, Await]:
hrow` UnaryExpression[?Yield, ?Await]
essionStatement[Yield, Await]:
ookahead ? {`{`, `function`, `async` [no |LineTerminator| here] `function`, `class`, `let [`, `throw`}] Expression[+In, ?Yield, ?Await] `;`
A throw
expression can be approximated in ECMAScript using something like the following definition:
t __throw = err => { throw err; };
ia helper...
tion getEncoder1(encoding) {
nst encoder = encoding === "utf8" ? new UTF8Encoder()
: encoding === "utf16le" ? new UTF16Encoder(false)
: encoding === "utf16be" ? new UTF16Encoder(true)
: __throw(new Error("Unsupported encoding"));
ia arrow...
tion getEncoder2(encoding) {
nst encoder = encoding === "utf8" ? new UTF8Encoder()
: encoding === "utf16le" ? new UTF16Encoder(false)
: encoding === "utf16be" ? new UTF16Encoder(true)
: (() => { throw new Error("Unsupported encoding"); })();
However, this has several downsides compared to a native implementation:
__throw
helper will appear in err.stack
in a host environment.Error.captureStackTrace
throw
is not local to the function.The following is a high-level list of tasks to progress through each stage of the TC39 proposal process: