我有一个方法,其代码可以引发ArgumentException。我做了一个扩展Exception的自定义异常,我想将内部异常设置为原始异常的那个异常抛出该异常,如下所示:
private void ConvertOldFormulary()
{
try
{
if (_instance.NewScriptFormat)
{
_oldFormulary = _instance.OldFormularyData.AsEnumerable().Select(x => new Item(
x.Field<string>(3), x.Field<string>(1), x.Field<string>(2), x.Field<string>(5), x.Field<string>(7),
x.Field<string>(11), x.Field<string>(14), x.Field<string>(16), x.Field<string>(17),
x.Field<string>(21))).ToList().ToDictionary(x => x.ItemId, x => x);
}
else
{
_oldFormulary = _instance.OldFormularyData.AsEnumerable().Select(x => new Item(
x.Field<string>(1), x.Field<string>(3), x.Field<string>(5), x.Field<string>(6), x.Field<string>(8),
x.Field<string>(12), x.Field<string>(15), x.Field<string>(17), x.Field<string>(18),
x.Field<string>(22))).ToList().ToDictionary(x => x.ItemId, x => x);
}
}
catch(ArgumentException e)
{
throw new DuplicateMedIdException("Unable to scan, old formulary contains duplicate IDs!", e);
}
}
来自Java背景,这对我来说非常有意义。此方法由我的Scanner类的构造函数调用,如下所示:
public Scanner(Main instance, BackgroundWorker worker)
{
_instance = instance;
_worker = worker;
ConvertDataTablesToDictionaries();
}
private void ConvertDataTablesToDictionaries()
{
var oldThread = new Thread(ConvertOldFormulary);
var newThread = new Thread(ConvertNewFormulary);
oldThread.Start();
newThread.Start();
oldThread.Join();
newThread.Join();
}
我希望该自定义异常在调用堆栈中传播并得到更高的处理。目前,这只是停止执行,告诉我未处理我的自定义异常。如何像在Java中一样,将这种自定义异常传播到Scanner类的调用者?