Java : DOMException (XML) with Examples
DOMException (Java SE 23 & JDK 23) in Java with Examples.
You will find code samples for most of the DOMException methods.
Summary
DOM operations only raise exceptions in "exceptional" circumstances, i.e., when an operation is impossible to perform (either for logical reasons, because data is lost, or because the implementation has become unstable). In general, DOM methods return specific error values in ordinary processing situations, such as out-of-bound errors when using NodeList.
final var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var document = builder.newDocument();
final var root = document.createElement("root");
document.appendChild(root);
try {
root.appendChild(root);
} catch (DOMException e) {
System.out.println(e);
}
// Result
// ↓
//org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR:
// An attempt was made to insert a node where it is not permitted.
Fields
short code
final var e = new DOMException(DOMException.INVALID_STATE_ERR, "abc");
System.out.println(e); // org.w3c.dom.DOMException: abc
System.out.println(e.code); // 11
static final short DOMSTRING_SIZE_ERR
If the specified range of text does not fit into a DOMString.
System.out.println(DOMException.DOMSTRING_SIZE_ERR); // 2
static final short HIERARCHY_REQUEST_ERR
If any Node is inserted somewhere it doesn't belong.
System.out.println(DOMException.HIERARCHY_REQUEST_ERR); // 3
static final short INDEX_SIZE_ERR
If index or size is negative, or greater than the allowed value.
System.out.println(DOMException.INDEX_SIZE_ERR); // 1
static final short INUSE_ATTRIBUTE_ERR
If an attempt is made to add an attribute that is already in use elsewhere.
System.out.println(DOMException.INUSE_ATTRIBUTE_ERR); // 10
static final short INVALID_ACCESS_ERR
If a parameter or an operation is not supported by the underlying object.
System.out.println(DOMException.INVALID_ACCESS_ERR); // 15
static final short INVALID_CHARACTER_ERR
If an invalid or illegal character is specified, such as in an XML name.
System.out.println(DOMException.INVALID_CHARACTER_ERR); // 5
static final short INVALID_MODIFICATION_ERR
If an attempt is made to modify the type of the underlying object.
System.out.println(DOMException.INVALID_MODIFICATION_ERR); // 13
static final short INVALID_STATE_ERR
If an attempt is made to use an object that is not, or is no longer, usable.
System.out.println(DOMException.INVALID_STATE_ERR); // 11
static final short NAMESPACE_ERR
If an attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
System.out.println(DOMException.NAMESPACE_ERR); // 14
static final short NO_DATA_ALLOWED_ERR
If data is specified for a Node which does not support data.
System.out.println(DOMException.NO_DATA_ALLOWED_ERR); // 6
static final short NO_MODIFICATION_ALLOWED_ERR
If an attempt is made to modify an object where modifications are not allowed.
System.out.println(DOMException.NO_MODIFICATION_ALLOWED_ERR); // 7
static final short NOT_FOUND_ERR
If an attempt is made to reference a Node in a context where it does not exist.
System.out.println(DOMException.NOT_FOUND_ERR); // 8
static final short NOT_SUPPORTED_ERR
If the implementation does not support the requested type of object or operation.
System.out.println(DOMException.NOT_SUPPORTED_ERR); // 9
static final short SYNTAX_ERR
If an invalid or illegal string is specified.
System.out.println(DOMException.SYNTAX_ERR); // 12
static final short TYPE_MISMATCH_ERR
If the type of an object is incompatible with the expected type of the parameter associated to the object.
System.out.println(DOMException.TYPE_MISMATCH_ERR); // 17
static final short VALIDATION_ERR
If a call to a method such as insertBefore or removeChild would make the Node invalid with respect to "partial validity", this exception would be raised and the operation would not be done.
System.out.println(DOMException.VALIDATION_ERR); // 16
static final short WRONG_DOCUMENT_ERR
If a Node is used in a different document than the one that created it (that doesn't support it).
System.out.println(DOMException.WRONG_DOCUMENT_ERR); // 4
Constructors
DOMException (short code, String message)
final var e = new DOMException(DOMException.INVALID_STATE_ERR, "abc");
System.out.println(e); // org.w3c.dom.DOMException: abc
System.out.println(e.code); // 11
Methods declared in Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
Please see the link below.