我目前正在尝试重新考虑一个基于异常类型预先执行某些操作的方法,当然会导致很多if else条件,在此方法中代码库看起来像这样
if (exception is DataException)
{
jobInformation.StatusDescription.DescriptionResourceReference =
new TextResourceReference(
typeof(StorageDevices.UserMessageTexts),
UserMessageDispatcher.DataError);
jobInformation.StatusDescription.SetParameters(
new object[] { exception.Message });
}
else if (exception is InsufficientMemoryException)
{
jobInformation.StatusDescription.DescriptionResourceReference =
new TextResourceReference(
typeof(StorageDevices.UserMessageTexts),
UserMessageDispatcher.InSufficientSpaceInMediaError
);
}
else if (exception is FileNotFoundException)
{
if (exception.Message.Contains("DicomViewer"))
{
jobInformation.StatusDescription.DescriptionResourceReference =
new TextResourceReference(
typeof(StorageDevices.UserMessageTexts),
UserMessageDispatcher.NoDicomViewer
);
}
else
{
jobInformation.StatusDescription.DescriptionResourceReference =
new TextResourceReference(
typeof(StorageDevices.UserMessageTexts),
UserMessageDispatcher.NoMediaOrSourceFileMissing
);
}
}
else (exception is ImapiException)
{
jobInformation.StatusDescription.DescriptionResourceReference =
new TextResourceReference(
typeof(StorageDevices.UserMessageTexts),
UserMessageDispatcher.MediaBurnError
);
}
很明显,在上面的代码库中,可以看出很少有例外是自定义的,而其他例外是.net例外。
现在我想以这样的方式重新考虑这个方法,以便我可以调用
exception.SetJobInformation(jobInformation)
并将相应的异常逻辑移到异常类中,但是.net异常类的可用性对我构成了挑战,对这种情况有什么建议或解决方案吗?