W3C home > Mailing lists > Public > public-script-coord@w3.org > April to June 2012

Re: Should Exceptions be Errors in the ECMAScript bindings?

From: Allen Wirfs-Brock <allen@wirfs-brock.com>
Date: Fri, 22 Jun 2012 08:40:03 -0700
Cc: Travis Leithead <Travis.Leithead@microsoft.com>, Erik Arvidsson <arv@chromium.org>, "public-script-coord@w3.org" <public-script-coord@w3.org>
Message-Id: <74F37B50-BCC6-428A-A384-5FE169B4A73A@wirfs-brock.com>
To: Cameron McCormack <cam@mcc.id.au>

On Jun 21, 2012, at 11:43 PM, Cameron McCormack wrote:

> Allen Wirfs-Brock:
>> In ECMAScript, prototype chain inheritance does not establish a deep semantic "is-a" relationship. In particular,
>>      var foo=Object.create(Error.prototype)
>> does not give foo any special Error object internal semantic state or behavior (eg, implementation specific stack trace semantics).
> What would be required to make exception objects be true Error objects?  Is it sufficient to have Web IDL just say that they have [[Class]] "Error"?  Do Error objects have any standardised behaviour that you wouldn't get from just using prototype inheritance as Web IDL currently does?

It seems  (I need to research this a bit more to be sure) like what should work  is to simply define your exception objects just like the ES spec defines "NativeError" objects[1].  The NativeErrors are all essentially "subclasses" of Error of the exactly the sort you will get with the proposed ES6 class extensions if you said [2]:
  class DOMException extends Error.prototype {
      constructor(...args) {super(...args}

You can wire up essentially the same structure manually which is what the ES5 spec. does for the NativeErrors.

Note that while there isn't currently any  special standardized behavior for Error, implementations do have non-standard behaviors such as capturing stack traces.  This would have to be dealt with in the actual implementations but conceptually is captured by the super constructor call. 


[1] http://ecma-international.org/ecma-262/5.1/#sec-15.11.7 
[2] there is a subtle reason why I had to say "extends Error.prototype" rather than "extends Error".
Received on Friday, 22 June 2012 15:40:40 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 17:14:06 UTC