I have a method parsing a file. However, this parsing could fail anytime, depending on various conditions (not-so-cautious user playing with the file, for example).
public string ParseDatFile(string datFile)
{
string[] deezLines = File.ReadAllLines(datFile);
// We're searching for an essential data inside the file.
bool daEssentialDataFound = false;
foreach (string datLine in deezLines)
{
if (datLine.Contains("daEssentialData"))
{
daEssentialDataFound = true;
break;
}
}
if (!daEssentialDataFound)
throw new WhatShouldIThrowException("yo dood where's da essential data in " + datFile + "?");
DoStuffWith(deezLines);
}
Is there an exception I could use in such a scenario? I thought about:
FileFormatException should be fine :
The exception that is thrown when an input file or a data stream that is supposed to conform to a certain file format specification is malformed.
You can optionally provide the uri and a descriptive error message.
If you don't want to reference WindowsBase
then you may create your own exception specific to your format. Based on the fact there is an XmlException
thrown by XmlReader.Read
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With