DOMException | NN 6 IE n/a DOM 1
Some operations on W3C DOM objects can trigger errors, or, in the vernacular of JavaScript 1.5, throw exceptions, if something goes wrong. The W3C DOM defines an object that conveys a code number corresponding to a well-defined (if limited) list of exceptions. For example, if you attempt to append one text node as a child of another text node, the appendChild( ) method of such an operation throws an exception whose code number is 3. This number corresponds to the exception that signals an attempt to perform an illegal or logically impossible action on a DOM hierarchy (a text node can't have any child nodes).
The job of conveying the DOM exception information to a scripter falls to the hosting environment, rather than the DOM. Because JavaScript 1.5 already has an exception handling mechanism, the task of blending the DOMException system with JavaScript exception handling fell first to Netscape, as implemented in Netscape 6. The new mechanism permits different kinds of error objects to circulate through the exception handling operations, thus leaving the original system intact, while extending the mechanism to accommodate not only the W3C DOM DOMException object, but some Netscape-specific errors, as well. Processing of exceptions of all kinds continues to take place in the catch block of a try/catch construction, and all information about the exception is still passed as an object through a single parameter to the catch block.
Netscape's DOM exception object (which embodies the W3C DOMException object) arrives at the catch block with a longer list of properties and methods associated with it than does an exception arising from other causes (e.g., trying to use a JavaScript variable that has not been initialized). The distinguishing property of a DOMException object, missing from the other types, is the code property. Moreover, any code value between 1 and 15 indicates an exception type known to the formal DOM specification through Level 2. Others will certainly be added to the list in the future. Netscape uses code numbers starting with 1000 for its list of browser-specific exceptions.
If you wish to process true W3C DOM exceptions along their own execution path, you can use a construction similar to the following (which allows for the DOMException list to grow to 999 in future iterations):
    try {
        // your DOM-related statement goes here
    }
    catch(e) {
        if (typeof e.code == "number") {
            if (e.code< 1000) {
                // process DOMException object here
            } else {
                // process Netscape DOM exception object here
            }
        } else {
            // process language or other exceptions here
        }
    }
Of course, it is highly unlikely that exception details will be of benefit to users, but they are invaluable to you during development. For more on exception handling, see the error object in Chapter 12.
Object Model Reference
    errorObjectReference
Object-Specific Properties
code
Object-Specific Methods
None.
Object-Specific Event Handler Properties
None.

code | NN 6 IE n/a DOM 1
| Read-only
Provides an integer corresponding to one of the defined DOM error types. The following table lists all code values, their constant equivalents, and examples of what kinds of problems throw the exception.
Code | Constant | Most likely cause
--- | --- | ---
1 | INDEX_SIZE_ERR | An integer offset parameter is out of the range of the target object
2 | DOMSTRING_SIZE_ERR | Property string value is too large for the hosting language
3 | HIERARCHY_REQUEST_ERR | Appending a child to a node not capable of children
4 | WRONG_DOCUMENT_ERR | Inserting a node created from a different document (without passing through the import process)
5 | INVALID_CHARACTER_ERR | Assigning an identifier with an illegal character
6 | NO_DATA_ALLOWED_ERR | Assigning data to a node that doesn't allow data
7 | NO_MODIFICATION_ALLOWED_ERR | Assigning a value to a read-only property
8 | NOT_FOUND_ERR | Method parameter reference to a nonexistent node in the object's scope
9 | NOT_SUPPORTED_ERR | Invoking an XML-only method in an HTML document
10 | INUSE_ATTRIBUTE_ERR | Method parameter to an Attr node that already belongs to another element (without cloning the Attr first)
11 | INVALID_STATE_ERR | Referencing a node that is not readable or writable
12 | SYNTAX_ERR | A slippery keyboard
13 | INVALID_MODIFICATION_ERR | Modifying the type of a node
14 | NAMESPACE_ERR | Namespace mismatch or malformed name
15 | INVALID_ACCESS_ERR | You can't go there
Example
    if (e.code == e.INVALID_CHARACTER_ERR) {
        // process for an illegal identifier character
    }
Value
Integer
Default
Determined by error.