CreateDocument(string templatePath)
{
Document doc = OpenDocument(templatePath);
Picture pic = GetLogo();
AddLogo(doc, pic, templatePath);
}
AddLogo(Document doc, Picture logo, string templatePath)
{
Picture placeholder = doc.FindLogoPlaceholder();
if (placeholder.Size != logo.Size)
{
throw new ApplicationException(
String.Format("Invalid template {0}, logo size: {1}, required: {2}",
templatePath, placeholder.Size, logo.Size
));
}
}
Consider the above code as an example I just made up.
Notice that the only reason templatePath is passed into the AddLogo method is to facilitate exception generation.
I have something in my code today where I needed to do this, and it feels like a really nasty code smell to me. But I’m not too familiar with exception handling patterns and I don’t really see any better way to do it.
I’m wondering what your thoughts are and if there is a better pattern to dealing with situations like this.
Create the exception on a higher level: