Q:

What exception to throw on encountering unexpected text?

I'm reading through an XML file and at a certain position I am expecting (as part of the contract) to read <offset>nnnn</offset> (where nnnn is a number). I have already checked that it is valid, well-formed XML. But if it does not say <format> at that point then it is breaking the contract and I want to throw an exception. What pre-existing Java exception class is the most appropriate one to throw in this case?

A:

Thanks for the answers given so far. I've just thought of my own answer: InputMismatchException. Advantage: its name describes exactly what the problem is. Disadvantage: its Javadoc says it is thrown by a Scanner, which is not what I'm using. Nevertheless, it does seem to be an appropriate choice. If anyone thinks this is not an appropriate exception class to use, please comment.

