I have a method whose code can throw an ArgumentException. I made a custom exception that extends Exception and I would like to throw that one with the inner exception set to the original one like below :
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);
}
}
Coming from a Java background, this makes perfect sense to me. This method is called by my Scanner class' constructor as below :
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();
}
I would like that custom exception to be propagated up the call stack and be handled higher. Currently this just stops the execution telling me my custom exception is unhandled. How do I propagate this custom exception to the Scanner class' caller as you can do in Java?