Can IllegalStateException be used to catch all helper exceptions?

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;

    // Setup document builder
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
            .newInstance();

    if (docBuilderFactory == null)
        throw new IllegalStateException(
                "The DocumentBuilderFactory cannot be instanciated");
    try {
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

        // Parse document
        Document doc = docBuilder.parse(inXMLDocument);

        if (doc != null)
            isXMLDocWellFormed = true;

    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        throw new IllegalStateException(e);
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        throw new IllegalStateException(e);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        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 :

  • , XML- .
  • , (- , ), .

b) a) , ?

,

+5
3

API, , . , . .

, IllegalStateException . ( IO - ?). - .

node - , , , .

0

- , . , - . . , , - ? , , ( ), , , , , . , ( ), . , , .

, :-) , , , , Java. , , , genralised . , API .

+2

, , , :

  • CleanFailureException: , . , , , , , . : . , , , , ( , , - ).
  • PartialOperationException: , . , , , , , , , .
  • StateCorruptException: , , , , , .
  • SystemOnFireException: .

An exception thrown by something like an array index outside the limits can fall into any of the above categories. The important thing is not that access to the index is out of bounds, but rather what it means about other objects. Catch, wrap, and rethrow would be a very useful model, since resolving an exception to one of the above categories would give the caller a much better idea of ​​what to do with him.

0
source

All Articles