Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4007990
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:41:57+00:00 2026-05-20T08:41:57+00:00

Is it possible to manually throw a DOMException error in pure JavaScript? Documentation I’ve

  • 0

Is it possible to manually throw a DOMException error in pure JavaScript? Documentation I’ve read suggests it should be relatively easy to construct (at least in Java.)

However, in Chrome, following code returns TypeError: Illegal constructor:

// DOM SYNTAX_ERR (12)
var myDOMException = new DOMException(12,"I'm sorry Dave, I'm afraid I can't do that.");

Regrettably, this is what I expected after reading the W3 docs, 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.)

Frustratingly, the DOMException class lurks tantalisingly in the global scope. How can I use it? Can I use it?

Update

Since I wrote this, I’ve made a couple of discoveries – namely:

var myDOMException = DOMException.constructor(12,"Error Message");
var myDOMException2 = DOMException.constructor.call(DOMException,DOMException.SYNTAX_ERR,"Error Message");

Looks like it worked!

…not so fast.

$> myDOMException instanceof DOMException
false
$> myDOMException2 instanceof DOMException
false

And possibly even more offputting:

$> myDOMException.constructor
function Number() {
    [native code]
}

As always, any assistance would be greatly appreciated.

Update #2

Just to clarify my reasons for returning a DOMException object as opposed to a more generic Error – I’m trying to implement the WHATWG’s Timed Text Track spec 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.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:41:57+00:00Added an answer on May 20, 2026 at 8:41 am

    In Firefox, at least, DOMException is not a function. It is an object that defines several constants.

     typeof DOMException === 'object' // true (not 'function')
    

    It could be used like this:

    try {
        throw DOMException;
    } catch(e) {
        if (e === DOMException)
            console.log("caught DOMException")
    }
    

    This works if you’re trying to signal a DOMException but don’t need an actual instance of DOMException.

    Ugly, ugly hack (that basically works)

    If you absolutely need an instance of DOMException that has the SYNTAX_ERR code, you could perform an action that causes one to be created and throw that:

    function createSyntaxException() {
        try {
            // will cause a DOMException
            document.querySelectorAll("div:foo");
        } catch(e) {
            return e;
        }
    }
    
    throw createSyntaxException();
    

    The details of the thrown exception won’t match your specific situation, of course, but the resulting object will have the correct code and pass instanceof checks.

    var e = createSyntaxException();
    console.log(e instanceof DOMException); // true
    console.log(e.code === e.SYNTAX_ERR); // true
    

    You could mitigate the details issue by subclassing DOMException and adding getters/setters for each of its (read-only) properties.

    function DOMExceptionCustom() {
        var message;
        this.__defineGetter__("message", function(){
            return message;
        });
        this.__defineSetter__("message", function(val){
            message = val;
        });
    }
    
    // subclass DOMException
    DOMExceptionCustom.prototype = createSyntaxException();
    
    var err = new DOMExceptionCustom();
    err.message = "my custom message";
    

    The resulting object has the desired properties:

    console.log(err.code === err.SYNTAX_ERR); // true
    console.log(err.message); // "my custom message"
    console.log(err instanceof DOMExceptionCustom); // true
    console.log(err instanceof DOMException); // true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.