You are here: Reference > JavaScript > client-side > HTML DOM > objects > DOMException

DOMException object

Browser support:
9
Represents a run-time error caused by a DOM operation.
Note: The DOMException object is supported in Internet Explorer from version 9.
When a script error occurred, JavaScript interpreters throw an Error object or DOMException object, depending on the type of the error. In most cases, an error is represented by an Error object, but in some cases when the error is caused by a DOM operation, JavaScript interpreters throw a DOMException object, not an Error object. The DOMException object is not supported in Internet Explorer before version 9, errors are always represented by Error objects in those browsers.

The Error and DOMException objects may be caught with the try...catch statement, and contain information about the occurred error.

The base interface, through which you can add new functionalities to the DOMException object, is the DOMException interface.

Possible members:

Constants:
The following constants are available in the scope of the DOMException object.
You can use them directly through the DOMException interface (DOMException.INDEX_SIZE_ERR) or through an instance of the DOMException object. The constants represent the possible error codes. You can use them for the value of the code property.
Using the constants instead of their numeric values results in more readable code.
INDEX_SIZE_ERR
1 The index or size is negative or greater than the allowed amount
DOMSTRING_SIZE_ERR
2 The text is too large to represent in a String.
HIERARCHY_REQUEST_ERR
3 The node cannot be inserted at the specified point in the hierarchy. The parent node cannot have children or it is the descendant of the node to insert.
WRONG_DOCUMENT_ERR
4 The node cannot be inserted, because it belongs to another document. Use the adoptNode or importNode method on foreign elements first.
INVALID_CHARACTER_ERR
5 The string contains an invalid character. For example when an invalid value is used for the createElement or createAttribute methods.
NO_DATA_ALLOWED_ERR
6 Data is specified for a node that doesn't support data
NO_MODIFICATION_ALLOWED_ERR
7 Attempted to modify an object where modifications are not allowed.
NOT_FOUND_ERR
8 Node was not found. For example when the removeChild method is used with a node that is not a child of the reference node.
NOT_SUPPORTED_ERR
9 The implementation does not support the requested operation.
INUSE_ATTRIBUTE_ERR
10 Attribute already in use. The attribute cannot be inserted because it is already in use on another element.
INVALID_STATE_ERR
11 The object cannot be used in its current state.
SYNTAX_ERR
12 The string is invalid for the current operation.
INVALID_MODIFICATION_ERR
13 The modification cannot be performed.
NAMESPACE_ERR
14 An attempt was made to create or change an object in a way which is incorrect with regard to namespaces
INVALID_ACCESS_ERR
15 The operation is not supported by the current object.
VALIDATION_ERR
16 The operation cannot be performed because the node would become invalid.
TYPE_MISMATCH_ERR
17 The type of the object is incompatible with the requested type.
SECURITY_ERR
18 Because of security restrictions, the operation cannot be performed on documents that are located in different domains.
NETWORK_ERR
19 A network error has occurred.
ABORT_ERR
20 The current operation has been aborted by the user.
URL_MISMATCH_ERR
21 The URL does not match.
QUOTA_EXCEEDED_ERR
22 The operation would exceed storage limits.
Properties:
code
Returns an integer that specifies the type of the error.

This property is read/write.
message
Returns a string that contains information about the error.

This property is read/write.
name
Returns an string that contains the type of the error in string format.

This property is read/write.

Example HTML code 1:

This example illustrates the use of the DOMException object:
<head>
    <script type="text/javascript">
        function ThrowDOMException () {
            try {
                    // INVALID_CHARACTER_ERR
                var elem = document.createAttribute ("123");
            }
            catch (e) {
                if (e.code == DOMException.INVALID_CHARACTER_ERR) {
                    alert ("The attribute name is invalid");
                }
                return;
            }
                // Internet Explorer before version 9
            alert ('Your browser does not throw an exception when invalid attribute name is used');
        }
    </script>
</head>
<body>
    <button onclick="ThrowDOMException ()">Throw a DOM exception</button>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

When DOM operations cause exceptions:
<head>
    <script type="text/javascript">
        function Init () {
            var message = "";

            try {
                var textNode = document.createTextNode ("some text");
                    // INDEX_SIZE_ERR
                textNode.splitText (1000);
            }
            catch (e) {
                message += e.message + "<br/>";
            }

            try {
                var childDiv = document.getElementById ("child");
                var grandchildDiv = document.getElementById ("grandchild");
                    // HIERARCHY_REQUEST_ERR
                grandchildDiv.appendChild (childDiv);
            }
            catch (e) {
                message += e.message + "<br/>";
            }

            try {
                    // INVALID_CHARACTER_ERR
                var elem = document.createAttribute ("123");
            }
            catch (e) {
                message += e.message + "<br/>";
            }


            try {
                var container = document.getElementById ("container");
                var grandchildDiv = document.getElementById ("grandchild");
                    // NOT_FOUND_ERR
                container.removeChild (grandchildDiv);
            }
            catch (e) {
                message += e.message + "<br/>";
            }

            try {
                var container = document.getElementById ("container");
                var grandchildDiv = document.getElementById ("grandchild");
                var div = document.createElement('div'); 
                    // NOT_FOUND_ERR
                container.insertBefore (div, grandchildDiv);
            }
            catch (e) {
                message += e.message + "<br/>";
            }


            try {
                var container = document.getElementById ("container");
                var titleAttr = container.getAttributeNode ("title");
                var childDiv = document.getElementById ("child");
                    // INUSE_ATTRIBUTE_ERR
                childDiv.setAttributeNode (titleAttr);
            }
            catch (e) {
                message += e.message + "<br/>";
            }



            var info = document.getElementById ("info");
            info.innerHTML = message;
        }
    </script>
</head>
<body onload="Init ()">
    <div id="container" title="a simple title" >
        <div id="child">
            <div id="grandchild">
            </div>
        </div>
    </div>
    <div id="info"></div>
</body>
Did you find this example helpful? yes no

Related pages:

External links:

User Contributed Comments

Post Content

Post Content