Re: Constructible Exceptions

Marcos Caceres:
> Yeah.. I kinda realised that after I sent the email… however…. how do you create one without first coercing an exception object out of the exception type you want? I had a go at trying to do it [1]… but I had to do the following for a DOMException:
>
> try {
>          //force an exception to be generated;
>          document.removeChild({})
>   } catch (e) {
>          //use it as the prototype
>       newException = Object.create(e)
> }
>
>
> //alias for the prototype, helps us populate the new object
> var proto = newException.__proto__
>
> //"shadow" the name property on the new object
> //Chrome claims these are writable, but they aint! :)
> props = Object.getOwnPropertyDescriptor(proto, "name");
> props.value = name;
> Object.defineProperty(newException, "name", props);

I'm not really sure what you're doing here.  Do you want to construct a 
new DOMException even though the implementation does not yet allow `new 
DOMException()`?

Note that you want to end up with an object whose prototype is 
DOMException.prototype, not a DOMException instance.  But before the 
underlying DOM implementation supports constructable DOMException 
objects you're not going to be able to create one just by using 
Object.create, and that's because things on DOMException.prototype 
expect the instance to be a platform exception object, and from the 
perspective of the DOM implementation your object you have created with 
Object.create() is not a platform exception object.

Received on Sunday, 25 March 2012 00:40:12 UTC