Code example for DOMException

0
        // Make sure we have at most one root element and one DTD element. 
        if (newChild instanceof Element && getDocumentElement() != null) {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
                    "Only one root element allowed"); 
        } else if (newChild instanceof DocumentType && getDoctype() != null) {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
                    "Only one DOCTYPE element allowed"); 
        } 
 
        return super.insertChildAt(newChild, index);
    } 
 
}