I wonder if using IllegalStateException in this scenario is a good design choice for the API.
Scenario: I have a function that checks if an XML document is well formed with the following signature:
public boolean isXMLDocumentWellFormed(InputStream inXMLDocument)
This function uses the following snippet to load an XML document:
boolean isXMLDocWellFormed = false;
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
if (docBuilderFactory == null)
throw new IllegalStateException(
"The DocumentBuilderFactory cannot be instanciated");
try {
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(inXMLDocument);
if (doc != null)
isXMLDocWellFormed = true;
} catch (ParserConfigurationException e) {
e.printStackTrace();
throw new IllegalStateException(e);
} catch (SAXException e) {
e.printStackTrace();
throw new IllegalStateException(e);
} catch (IOException e) {
e.printStackTrace();
throw new IllegalStateException(e);
}
return isXMLDocWellFormed;
The API is designed to be very easy to use. However, I would like to give API users the opportunity to find out why an XML document cannot be validated (for example: DocumentBuilderFactory cannot create a DocumentBuilder object) without suppressing them with the number of checked exceptions that they will have to deal with.
My concerns regarding this design:
a) (.. IllegalStateException), , ? "", 2 :
b) a) , ?
,