I'm not quite sure how to pose this questions. I really hope I can explain what my problem is properly.
I have multiple custom-exceptions distributed in my windows-service which I want to give back to the main process which initiates the process steps.
I have the static class PropertyMapper for example, which uses reflection to read email-headers and reflects them unto specific properties. Now if an exception turns up I want to add additional Information. Like what headerattribute actually caused the exception, but I don't want to lose the "actual" exception.
Currently it looks like this:
try
{
PropertyInfo pInfo = prop.Property;
Type t = Nullable.GetUnderlyingType(pInfo.PropertyType) ?? pInfo.PropertyType;
object safeValue = (headerAttribute.Value == null) ? null : Convert.ChangeType(headerAttribute.Value, t);
pInfo.SetValue(mailDataObj, safeValue, null);
}
catch (Exception e)
{
throw new PropertyMappingFailedException(headerAttribute.Field, headerParam);
}
I throw the Exception to carry it back to the main process so that I only have to implement the "create logEntry-logic" once. ´
try
{
mailDataObj = PropertyMapper.MapProperties(mailItem);
mailDataObj.MailItem = mailItem;
controller = new DAKMailController(mailDataObj);
controller.SetBasicData(procFile);
controller.HandlePostProcessing();
}
catch (Exception e)
{
controller.CreateLogEntry(e);
moveFileToError(file);
}
Now of course the exception that I originally cought is lost, because I don't add it to my custom exception, but how do I do that anyways? Also, is my way of thinking right or do I have to process exceptions in another way?
I already googled a little, but wasn't able to found something helpful. I'd appreciate some help. :)
P.S. I extract all InnerExceotions inside of the CreateLogEntry-method and put them into a single string-var.