Conditional catch clause
brendan at mozilla.com
Wed Dec 19 11:14:54 PST 2012
David Bruant wrote:
> Le 19/12/2012 16:38, Claude Pache a écrit :
>> Le 19 déc. 2012 à 15:32, David Bruant <bruant.d at gmail.com> a écrit :
>>> Le 19/12/2012 14:13, Claude Pache a écrit :
>>>> In SpiderMonkey (and perhaps other JS engines?), there are
>>>> conditional catch clauses:
>>>> catch (exception if condition)
>>>> Could such a feature added to ECMAScript?
>>>> Rationale: Although try/catch mechanism was initially intended for
>>>> treating errors, it could be used in normal control flow with
>>>> non-error condition.
>>> Why not just use normal control flow when you want to express
>>> non-error conditions?
>>> throw/try/catch was indeed intented for treating errors and when I
>>> read code, I expect an error path when encountering a try/catch.
>> As I've said, iterators throw StopIteration, which may be considered
>> as a non-error.
> I completely agree. I have said elsewhere  that StopIteration could
> be its own statement. What we want is a way to stop the current call
> frame. The language provides 2 such ways, return and throw. Return
> can't be used because it may result in ambiguities. It could be the
> same thing for throw, but normal use of throw make that throwing a
> specific object is an acceptable hack on StopIteration.
This is a red herring. It's very rare to have to catch StopIteration.
The catch (e if cond) syntax is in SpiderMonkey and Rhino, dates from
ES3 where it was proposed, prototyped, and shot down in Ecma TC39 TG1,
but kept in the implementations. Again, this syntax helps discriminate
on e's duck type or instanceof without requiring a hand-coded if-else
chain or switch, with mandatory re-throw in final-else or default.
In TC39 we had a discussion, I think in March 2011, about generalized
refutable match as proposed by dherman:
This was considered not baked enough to put in ES6. The tension with
destructuring is an ongoing issue in my view. Also, Dave's strawman
changes catch from how it works in SpiderMonkey and Rhino, so a pattern
implies an 'if'.
I think we need to talk about refutable patterns and matching again,
before we beat the catch-if dead horse once again. Suggest new spin-off
thread (different subject).
More information about the es-discuss