DOMException (W3C DOM Core object)
var node = document.getElementsByTagName('h1').item(0); var refnode = node.nextSibling; var newnode = document.createTextNode('That is why you fail.'); node.insertBefore(newnode, refnode);
The example above shows an
insertBefore operation that will fail, because
refnode is not a child of node. That
operation produces the following exception in Opera:
Inline script thread Error: Unhandled Exception: [Object DOMException] code: 8 message: NOT_FOUND_ERR
DOMException interface represents a processing error,
for example when trying to create an invalid DOM, or passing a
non-existent node as an argument to node manipulation methods. An
exception is raised when an operation is impossible to perform, ie. in
The DOM does not define an exception for every possible error that might occur, for example syntax errors in arguments are not included, and in such cases an implementation will use its own error reporting mechanism to deal with them.
In practise, what we find is that most browsers implement DOM
exceptions as part of their native mechanism, passing the exception
code and message as the details of the error, along with any
additional information the browser is able to provide (such as line
Internet Explorer does not implement this
interface, and uses its native error reporting syntax only; for example,
the example above would throw
Invalid argument). In XML
a more informative description will be given — for this example,
Insert position Node must be a Child of the Node to insert
Safari 1.3 and 2 do not report the error
name, only the error code. However this is not documented as a bug
because the specification does not define error names as properties of
The interface defines
ExceptionCode integers — exposed through the
code property — to refer to each type of error;
these are defined by the following constants:
- If an index is negative, or larger than the allowed value; for
splitTextwith an offset argument larger than the length of the string.
- The specified character data is too large for a
DOMString. The size limit is implementation dependent, and not defined by the DOM (see DOM Core for details of each browser's limit).
- This node is not allowed to have children of the new node's
type, or the new node is already an ancestor of this node; for
example, attempting to append an
Elementnode to a
- Attempted to use a node in a document other than the one that
created it; for example, using
appendChildwith a reference to a child node from another
Document, rather than importing it first (using
- An illegal or invalid character is specified in a string that has limitations, such as an element name1.
- Data has been specified for a node that doesn't support data;
for example an
Elementnode does not itself contain data, it has child nodes which contain data.
- Attempted to modify a node that cannot be modified; for example, appending to a node that is readonly.
- The referenced node does not exist; for example using
insertBeforewith a child node that is not a child of the reference node.
- The implementation does not support the specified operation; for example, when attempting to use a method of a node that is known but not implemented, an implementation may throw this error.
- Attempted to add an attribute that's already in use; for
setAttributeNodewith a reference to an
Attrthat is already in use on another
Element, rather than cloning it first (using
- Attempted to use an object that is not useable; this error is generally only thrown by an implementation when it's not able to perform a specific operation for internal reasons.
- In invalid or illegal string has been specified; for example
selectorTextproperty of a
CSSStyleRulewith an invalid CSS value.
- Attempted to modify a node's type; for example, setting the
cssTextproperty of a
CSSRulewith a value that does not match the original rule's type (eg. setting style-rule values for an at-rule).
- An operation conflicts with the namespace rules; for example,
createElementNSwith a malformed qualified name2.
- A property or operation is not supported on the specified node;
for example, attempting to use the
CSSPrimitiveValueon a CSS property that doesn't contain a float value.
In this Section
1 Names in XML can contain letters, numbers, underscores, periods (full stop) and hyphens; they cannot begin with a number, period or hyphen. They can also contain colons, however since colons are used in namespaces their more general use is discouraged.