DOMException
Every error thrown by the DOM API — a guarded mutation on a read-only document, an invalid selector, a hierarchy violation — is a DOMException instance. The constructor is also available directly, for a binding that wants to raise its own DOM-shaped error.
Constructor
new DOMException(message?, name?)
| Parameter | Default | Description |
|---|---|---|
message |
"" |
Human-readable description. |
name |
"Error" |
The error name — one of the standard names below, or any custom string. |
throw new DOMException("no matching element", "NotFoundError");
Members
| Member | Value |
|---|---|
name |
The name passed to the constructor ("Error" if omitted). |
message |
The message passed to the constructor ("" if omitted). |
code |
The legacy numeric code for name, or 0 if name is not one of the recognised legacy names. |
toString
toString() → "DOMException: <name>", or "DOMException: <name>: <message>" when message is non-empty.
try {
card.setAttribute("id", "x"); // card is read-only
} catch (e) {
e instanceof DOMException; // true
e.name; // "NoModificationAllowedError"
e.code; // 7
e.toString(); // "DOMException: NoModificationAllowedError: document is read-only in this binding context"
}
Name and legacy code
code reflects the Web IDL legacy error code for the recognised name values:
name |
code |
|---|---|
IndexSizeError |
1 |
DOMStringSizeError |
2 |
HierarchyRequestError |
3 |
WrongDocumentError |
4 |
InvalidCharacterError |
5 |
NoDataAllowedError |
6 |
NoModificationAllowedError |
7 |
NotFoundError |
8 |
NotSupportedError |
9 |
InUseAttributeError |
10 |
InvalidStateError |
11 |
SyntaxError |
12 |
InvalidModificationError |
13 |
NamespaceError |
14 |
InvalidAccessError |
15 |
ValidationError |
16 |
TypeMismatchError |
17 |
SecurityError |
18 |
NetworkError |
19 |
AbortError |
20 |
URLMismatchError |
21 |
QuotaExceededError |
22 |
TimeoutError |
23 |
InvalidNodeTypeError |
24 |
DataCloneError |
25 |
Any other name (including the default "Error") yields code === 0.
Names raised by this API
The DOM API overview lists the specific name values this implementation actually throws — SyntaxError, HierarchyRequestError, NotFoundError, NamespaceError, and NoModificationAllowedError — and the conditions that raise each one.
See also
- JavaScript DOM API — which names this API raises, and when
- Node — the tree mutations that are guarded on a read-only document