Search code examples
.netxmlexceptionthrowxml-attribute

Is InvalidDataException appropriate to throw when an XML attribute has a bad value?


I parse an XML file to load data and when I have an attribute that has an unrecognized value I throw an InvalidDataException. Is this the correct one or is there a better/more descriptive one?


Solution

  • That exception suggests that it's an error in the format of the file itself, like an error in the encoding of the characters. An XmlException would be a better fit.

    Consider throwing an ApplicationException instead, as it's not really an error in the XML format, but rather something that the XML contains that your application does not accept.