Standardizing conditional try/catch

T.J. Crowder tj.crowder at farsightsoftware.com
Sat Mar 18 19:20:42 UTC 2017


I don't see optimization being a big deal, this is exception processing
after all.

I was also thinking any expression:

```js
try {
    // ...
}
catch (e if arbitraryExpression1Here) {
    // ...handle case 1
}
catch (e if arbitraryExpression2Here) {
    // ...handle case 2
}
```

...which would essentially be:

```js
try {
    // ...
}
catch (e) {
    if (arbitraryExpression1Here) {
        // ...handle case 1
    } else if (arbitraryExpression2Here) {
        // ...handle case 2
    } else {
        throw e;
    }
}
```

...but without messing up the reported source of the error/stack (I'm
looking at you, IE11).

-- T.J. Crowder


On Sat, Mar 18, 2017 at 6:06 PM, Jordan Harband <ljharb at gmail.com> wrote:

> If the condition can be just "any javascript", wouldn't that potentially
> impede optimizations? Hopefully implementors can weigh in here, since
> non-implementor performance intuition (like mine) is often off base.
>
> On Sat, Mar 18, 2017 at 9:16 AM, kdex <kdex at kdex.de> wrote:
>
>> I'm not sure if embedding this idea into the language will make future
>> ideas about true pattern matching harder to implement or not.
>> Destructuring assignments are pretty slow from what I've measured, and
>> they still made it in, so I hardly see performance being a showstopper here.
>>
>> On Saturday, March 18, 2017 12:18:22 PM CET Michael J. Ryan wrote:
>> > The if condition doesn't need to be limited to instance of...
>> >
>> > catch (err if !isNaN(err.status))
>> >
>> > Aside: entering code in a phone is hard...
>> >
>> > > `instanceof` doesn't work across realms (iframes, for example). If we
>> > > introduced conditional catch blocks, I'd want a more reliable matching
>> > > mechanism than instanceof.
>> > >
>> > > On Fri, Mar 17, 2017 at 5:01 PM, Zach Lym <zachlym at indolering.com>
>> wrote:
>> > >
>> > >> Firefox supports the following conditional `catch` syntax:
>> > >>
>> > >>     try {
>> > >>         let result = await ();
>> > >>     } catch (e if e instanceof ErrorType) {
>> > >>         ...
>> > >>     }
>> > >>
>> > >>
>> > >> This was originally implemented in Spidermonkey as part of an ES
>> proposal
>> > >> around 2000, but it was rejected for unknown reasons [0]. A 2012
>> email to
>> > >> this list suggesting standardization of the syntax was passed over
>> in favor
>> > >> of waiting for a generic pattern matching facility [0][1].  Later
>> > >> discussion suggests that the pattern matching proposal would have
>> been very
>> > >> slow [2]. A proposal for a Java-like type-based conditional was
>> proposed in
>> > >> 2016, but was criticized for lacking generality [2].
>> > >>
>> > >> If the above summary is accurate, I would like to try to standardize
>> the
>> > >> vanilla syntax once again.  It's imperative, general, and doesn't
>> preclude
>> > >> the use of any hypothetical pattern matching functionality.
>> > >>
>> > >> Javascript's control flow has improved dramatically in recent years:
>> > >> promises got rid of callbacks, `async`/`await` clipped promise
>> chains, and
>> > >> classes make it easy to create custom Error objects that preserve
>> > >> stacktraces.  Conditional catch is the last bit of syntax needed to
>> make JS
>> > >> look like it was designed to handle asynchronous functions.
>> > >>
>> > >> Thoughts?
>> > >>
>> > >> -Zach Lym
>> > >>
>> > >> [0]: https://esdiscuss.org/topic/conditional-catch-clause#content-10
>> > >> [1]: https://esdiscuss.org/topic/conditional-catch
>> > >> [2]: https://esdiscuss.org/topic/error-type-specific-try-catch-bl
>> > >> ocks#content-14
>> > >>
>> > >> _______________________________________________
>> > >> es-discuss mailing list
>> > >> es-discuss at mozilla.org
>> > >> https://mail.mozilla.org/listinfo/es-discuss
>> > >>
>> > >>
>> > >
>> > > _______________________________________________
>> > > es-discuss mailing list
>> > > es-discuss at mozilla.org
>> > > https://mail.mozilla.org/listinfo/es-discuss
>> > >
>> > >
>> >
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170318/5d2117e7/attachment.html>


More information about the es-discuss mailing list