DOMException object
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.
|
Properties:
Returns an integer that specifies the type of the error.
This property is read/write. |
|||||||
Returns a string that contains information about the error.
This property is read/write. |
|||||||
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?
|
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?
|
Related pages:
External links:
User Contributed Comments