0

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?

Martin
  • 1,977
  • 5
  • 30
  • 67
  • Let me know if those links don't work for you. _Be explicit about why they don't work._ – mjwills May 29 '20 at 01:02
  • Given the method is invoked _in a new thread_ where exactly do you want the exception to bubble up **to**? Does it work as expected if you avoid executing the method in new threads (I am not saying this is a long term solution, but please test it to be sure). – mjwills May 29 '20 at 01:03
  • you're right, it works as expected when not running in a separate thread. Is there no way to propagate an exception when a thread is involved? – Martin May 29 '20 at 01:16
  • Is using `Task`s rather than `Thread`s an option? https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/exception-handling-task-parallel-library – mjwills May 29 '20 at 05:00
  • I tried that exact code calling my 2 methods and it doesn't work. Visual studio halts on the line throwing my custom exception and will not go any further – Martin May 29 '20 at 12:19
  • Check the first duplicate link. – mjwills May 29 '20 at 22:53

0 Answers0