You've probably noticed that many of the methods so far have been declared to throw a DOMException . This class shown in Example 9.15 is the generic exception for essentially anything that can go wrong while working with DOMfrom logical errors like making an element one of its own children to implementation bugs . Although it is a runtime exception that does not have to be caught, I nonetheless recommend that you always catch it or declare that your method throws it. Conceptually, this should be a checked exception; however, many languages that DOM supports, including C++ and Python, don't have checked exceptions, so DOM uses runtime exceptions in order to keep the semantics of the various methods as similar as possible across languages. Example 9.15 The DOMException Classpackage org.w3c.dom; public class DOMException extends RuntimeException { public DOMException(short code, String message); public short code; public static final short INDEX_SIZE_ERR = 1; public static final short DOMSTRING_SIZE_ERR = 2; public static final short HIERARCHY_REQUEST_ERR = 3; public static final short WRONG_DOCUMENT_ERR = 4; public static final short INVALID_CHARACTER_ERR = 5; public static final short NO_DATA_ALLOWED_ERR = 6; public static final short NO_MODIFICATION_ALLOWED_ERR = 7; public static final short NOT_FOUND_ERR = 8; public static final short NOT_SUPPORTED_ERR = 9; public static final short INUSE_ATTRIBUTE_ERR = 10; public static final short INVALID_STATE_ERR = 11; public static final short SYNTAX_ERR = 12; public static final short INVALID_MODIFICATION_ERR = 13; public static final short NAMESPACE_ERR = 14; public static final short INVALID_ACCESS_ERR = 15; } DOMException is the only exception that DOM standard methods throw. DOM methods don't throw IOException , IllegalArgumentException , SAXException , or any other exceptions you may be familiar with from Java. In a few cases, the implementation classes may throw a different exception, especially NullPointerException ; and methods in non-DOM support classes such as org.apache. xerces.parser.DOMParser or javax.xml.transform.dom.DOMResult can most certainly throw these exceptions. However, the DOM methods themselves don't throw them. Not only do DOM methods only throw DOMException s. They don't even throw any subclasses of DOMException . Here, DOM is following C++ conventions rather than Java conventions. Whereas Java tends to differentiate related exceptions through many different subclasses, [2] DOM uses named short constants to identify the different problems that can arise. This is also useful for languages like AppleScript in which exceptions aren't even classes. The exception code is exposed through DOMException 's public code field. The codes are defined as follows :
Although there's no way for DOM to prevent programs from using error codes other than those listed here, the W3C has reserved all possible error codes for its own use. If you need something not listed here, I recommend writing your own exception class or subclass DOMException . ( Just because DOM doesn't make full use of an object-oriented exception mechanism for reasons of compatibility with languages less object-oriented than Java doesn't mean you shouldn't do this in your pure Java code.) |