我不太清楚如何提出這個問題。我真的很希望我能解釋我的問題是什麼。c#返回組合異常
我有多個自定義異常分佈在我的windows-service中,我想回饋給啓動過程步驟的主進程。
例如,我有靜態類PropertyMapper,它使用反射來讀取email-headers並將它們反映到特定的屬性。現在,如果出現異常,我想添加其他信息。像headerattribute實際上導致了異常,但我不想失去「實際」異常。
目前,它看起來像這樣:
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);
}
我拋出異常攜帶它返回到主過程,使我只需要貫徹「創建logEntry邏輯」一次。 '
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);
}
現在當然,我毫無遺漏原來丟失,因爲我不把它添加到我的自定義異常,但我怎麼做,反正例外呢?另外,我的思維方式是否正確,還是必須以其他方式處理異常?
我已經google了一下,但沒能找到有用的東西。我會很感激一些幫助。 :)
P.S.我提取了CreateLogEntry方法中的所有InnerExcectivity,並將它們放入單個字符串-var中。
但是,InnerException是隻讀的? – brocrow
啊,哦。是否有可能將內部異常作爲參數發送給構造函數(比您使用的另一個超載)? –
'拋出新的CustomException(「我的消息」,前)'這是你正在談論的stacktrace的語法是字符串 –