13.6.2.12 Exceptions
Description: Exceptions
Keywords: lib

###
13.6.2.12 Exceptions

New in version 2.1.

The DOM Level 2 recommendation defines a single exception, `DOMException`, and a number of constants that allow applications to determine what sort of error occurred. `DOMException` instances carry a `code` attribute that provides the appropriate value for the specific exception.

The Python DOM interface provides the constants, but also expands the set of exceptions so that a specific exception exists for each of the exception codes defined by the DOM. The implementations must raise the appropriate specific exception, each of which carries the appropriate value for the `code` attribute.

exception `DOMException`
  Base exception class used for all specific DOM exceptions. This exception class cannot be directly instantiated.

exception `DomstringSizeErr`
  Raised when a specified range of text does not fit into a string. This is not known to be used in the Python DOM implementations, but may be received from DOM implementations not written in Python.

exception `HierarchyRequestErr`
  Raised when an attempt is made to insert a node where the node type is not allowed.

exception `IndexSizeErr`
  Raised when an index or size parameter to a method is negative or exceeds the allowed values.

exception `InuseAttributeErr`
  Raised when an attempt is made to insert an `Attr` node that is already present elsewhere in the document.

exception `InvalidAccessErr`
  Raised if a parameter or an operation is not supported on the underlying object.

exception `InvalidCharacterErr`
  This exception is raised when a string parameter contains a character that is not permitted in the context it's being used in by the XML 1.0 recommendation. For example, attempting to create an `Element` node with a space in the element type name will cause this error to be raised.

exception `InvalidModificationErr`
  Raised when an attempt is made to modify the type of a node.

exception `InvalidStateErr`
  Raised when an attempt is made to use an object that is not or is no longer usable.

exception `NamespaceErr`
  If an attempt is made to change any object in a way that is not permitted with regard to the Namespaces in XML recommendation, this exception is raised.

exception `NotFoundErr`
  Exception when a node does not exist in the referenced context. For example, `NamedNodeMap.removeNamedItem()` will raise this if the node passed in does not exist in the map.

exception `NotSupportedErr`
  Raised when the implementation does not support the requested type of object or operation.

exception `NoDataAllowedErr`
  This is raised if data is specified for a node which does not support data.

exception `NoModificationAllowedErr`
  Raised on attempts to modify an object where modifications are not allowed (such as for read-only nodes).

exception `SyntaxErr`
  Raised when an invalid or illegal string is specified.

exception `WrongDocumentErr`
  Raised when a node is inserted in a different document than it currently belongs to, and the implementation does not support migrating the node from one document to the other.

The exception codes defined in the DOM recommendation map to the exceptions described above according to this table:

Constant | Exception
--- | ---
`DOMSTRING_SIZE_ERR` | `DomstringSizeErr`
`HIERARCHY_REQUEST_ERR` | `HierarchyRequestErr`
`INDEX_SIZE_ERR` | `IndexSizeErr`
`INUSE_ATTRIBUTE_ERR` | `InuseAttributeErr`
`INVALID_ACCESS_ERR` | `InvalidAccessErr`
`INVALID_CHARACTER_ERR` | `InvalidCharacterErr`
`INVALID_MODIFICATION_ERR` | `InvalidModificationErr`
`INVALID_STATE_ERR` | `InvalidStateErr`
`NAMESPACE_ERR` | `NamespaceErr`
`NOT_FOUND_ERR` | `NotFoundErr`
`NOT_SUPPORTED_ERR` | `NotSupportedErr`
`NO_DATA_ALLOWED_ERR` | `NoDataAllowedErr`
`NO_MODIFICATION_ALLOWED_ERR` | `NoModificationAllowedErr`
`SYNTAX_ERR` | `SyntaxErr`
`WRONG_DOCUMENT_ERR` | `WrongDocumentErr`