title: Manually/Artificially throwing a DOMException with JavaScript
body: <p>Is it possible to manually throw a DOMException error in pure JavaScript? <a href="http://www.cafeconleche.org/books/xmljava/chapters/ch09s10.html" rel="noreferrer">Documentation I've read</a> suggests it should be relatively easy to construct (at least in Java.)</p>
  
  <p>However, in Chrome, following code returns <code>TypeError: Illegal constructor</code>:</p>
  
  <pre><code>// DOM SYNTAX_ERR (12)
  var myDOMException = new DOMException(12,"I'm sorry Dave, I'm afraid I can't do that.");
  </code></pre>
  
  <p>Regrettably, this is what I expected after reading <a href="http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-17189187" rel="noreferrer">the W3 docs</a>, which don't appear to specify a constructor at all. (As an aside, while I'm not particularly 'au fait' with IDL, I would have assumed their variant would support specification of constructors.)</p>
  
  <p>Frustratingly, the DOMException class lurks tantalisingly in the global scope. How can I use it? <em>Can</em> I use it?</p>
  
  <p><strong>Update</strong></p>
  
  <p>Since I wrote this, I've made a couple of discoveries - namely:</p>
  
  <pre><code>var myDOMException = DOMException.constructor(12,"Error Message");
  var myDOMException2 = DOMException.constructor.call(DOMException,DOMException.SYNTAX_ERR,"Error Message");
  </code></pre>
  
  <p>Looks like it worked!</p>
  
  <p>...not so fast.</p>
  
  <pre><code>$&gt; myDOMException instanceof DOMException
  false
  $&gt; myDOMException2 instanceof DOMException
  false
  </code></pre>
  
  <p>And possibly even more offputting:</p>
  
  <pre><code>$&gt; myDOMException.constructor
  function Number() {
      [native code]
  }
  </code></pre>
  
  <p>As always, any assistance would be greatly appreciated.</p>
  
  <p><strong>Update #2</strong></p>
  
  <p>Just to clarify my reasons for returning a DOMException object as opposed to a more generic Error - I'm trying to implement the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#timed-text-tracks" rel="noreferrer">WHATWG's Timed Text Track spec</a> in pure JavaScript. There are a number of instances where a proper solution would be required to return a DOMException object, specifically one with a code of 12 (SYNTAX_ERR.)</p>