I have a process that is parsing an XML file.
This is occuring in the PAckage Class.
The Package class has a Delegate that sets the object to an invalid state and captures the detailed info on error that occured the Package Class
For simplicity I am showing the filitem being passed to the package..
i.e `
foreach( var package in Packages)
{
try
{
package.ProcessXml(fileitem.nextfile);
}
catch (CustomeErrorException ex)
{
Logger.LogError(ex)
}
}
Inside The Package my validations look something like this
var Album = xml.Descendants()
.Select(albumShards => new Album {
Label = (string)albumShards.Descendants(TempAlbum.LabelLoc).FirstOrDefault() == "" ?
FailPackage("Error on label Load",Componets.Package,SubComp.BuildAlbum ) : (string)albumShards.Descendants(TempAlbum.LabelLoc).FirstOrDefault()
On this validation I check to see if “” is returned for label… if so Call Failpackage with error info and create exception
protected override void FailPackage(string msg, LogItem logItem)
{
Valid = ProcessState.Bad;
Logger.LogError(msg,logItem);
throw CustomErrorException(msg, Logitem);
}
that is captured via the containing try catch block
My concern is that I am using Exceptions for Program flow … how else should I look at approaching this problem or is this a valid Pattern.
ProcessXml fails to do what its name implies in certain cases; these are error cases. Exceptions are for error handling, despite what the name implies.
Krzysztof Cwalina, Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries
In other words, you’re in the right. 🙂
Read the chapter on exceptions in the above book for some excellent guidelines.