Re: Catching exceptions

The ECAMScript specification uses Completion types http://ecma-international.org/ecma-262/5.1/#sec-8.9 to propagate and handle exceptions. In ES5, all statement level constructs of the language return completion values and in some cases they explicitly check the completion values returned from nested code.  For example see http://ecma-international.org/ecma-262/5.1/#sec-12.10 step 7.  

Also see the last paragraph of http://ecma-international.org/ecma-262/5.1/#sec-5.2 . This is how the ES5 spec. deals with built-in functions that call other functions that might throw.  they just assume that the exception is propagated until it is handled by something like the with statement referenced above.

The ES5 approach is rather sloppy and leaves too much room for implementations to have observable variation in their recognition of unexpected exceptions.

The ES6 spec is more explicit about exception handling.  It has an expanded definition of the Completion type http://people.mozilla.org/~jorendorff/es6-draft.html#sec-8.2.3 and expands to the use of Completion values to all types of evaluation, not just at the statement level. It also requires all algorithms to explicitly state their handling of propagated exceptions.  See for example the ES6 definition of Array forEach http://people.mozilla.org/~jorendorff/es6-draft.html#sec-15.4.4.18 and compare it to the ES5 definition http://ecma-international.org/ecma-262/5.1/#sec-15.4.4.18  



On Apr 2, 2013, at 2:38 AM, Anne van Kesteren wrote:

> So reading the ECMAScript specification I cannot find this concept
> used there. A number of scenarios require us to catch exceptions and
> either handle them or maybe re-throw them as appropriate. Is this
> language fine:
> 
> 1. Invoke /callback/ ...
> 
> 2. If that threw an exception, ...
> 
> Or are there some other things I need to take into account? Is there
> preferred language for this kind of pattern?
> 
> 
> --
> http://annevankesteren.nl/
> 

Received on Tuesday, 2 April 2013 15:50:07 UTC