”Exception unhandled by user code“ when using Parallel.Invoke



Question:

I got an exception here:
MyException was unhandled by user code
In fact I have tried this, but
What is the problem and how to resolve it? What I want to achieve is get the exception be catch by the try catch surrounded the parallel.All. Now, it does not aware of that try catch, and prompt me the exception is unhandled by user code. Noted that I require the TestParallel1 method to throw exception, as this is the simplified format of the program I have. Given an exception, I wish to stop all other threads immediately. Furthermore I wish the exception propagate outside the parallel.
namespace WindowsFormsApplication1  {      static class Program      {            public static void Main()          {              try              {                  List<Action> act = new List<Action>                        {                           ()=>TestParallel1(),                           () => TestParallel1()                        };                    Parallel.Invoke(act.ToArray());              }              catch (AggregateException ae)              {                  foreach (var e in ae.InnerExceptions)  // note the 's' in InnerExceptions                  {                      //do something with 'e'                  }                  //do something              }          }            public class MyException : Exception          {          }            public static void TestParallel1()          {              throw new MyException();          }      }  }  
enter image description here


Solution:2

To Catch this Exception and still use Parallel you could do this:
        List<Action> act = new List<Action> { () => TestParallel1(),                                                 () => TestParallel1() };            Parallel.ForEach(act, a =>{              try              {                  a.Invoke();              }              catch (MyException ae)              {                  //do something              }          });  

EDIT

Maybe something like this could do the trick if events and delegates are ok for you
i'm aware that this code could be look better but it will show you the idea so i hope it will help you :)
public partial class MainWindow : Window  {      List<MyThread> threads = new List<MyThread>();        public MainWindow()      {          var thread1 = new MyThread();          thread1.A = () => TestParallel1();          thread1.RaisError += RaisError;            var thread2 = new MyThread();          thread2.A = () => TestParallel1();              threads.Add(thread1);          threads.Add(thread2);            Parallel.ForEach(threads, t => { t.Start(); });      }        public void RaisError()      {          Parallel.ForEach(threads, t => { t.Stop(); });      }        public static void TestParallel1()      {          throw new MyException();      }  }    public class MyException:Exception{}      public class MyThread  {      public Action A { get; set; }      public delegate void Raiser();      public event Raiser RaisError;        public void Start()      {          try          {              A.Invoke();          }          catch (MyException me)          {              RaisError();          }      }        public void Stop()      {          // do some stop      }  }  

Edit 2

you may could also do this but you should read the This Answer(from Scott Chamberlain) first
        List<Action> act = new List<Action> { () => TestParallel1(),                                             () => TestParallel1() };            Parallel.ForEach(act, (a, state) =>          {              try              {                  a.Invoke();              }              catch (MyException ae)              {                  state.Stop();              }          });  


Solution:3

You are getting the "unhandled by user code" in the debugger because you have "Just my code" setting turned on (and the original exception isn't technically being handled by your code, but rather being handled by the framework's code).
Turn off "Just my code", and you'll get a different experience.
Note, this is just debugging experience--it doesn't have any effect on the regular program flow.
See: http://msdn.microsoft.com/en-us/library/h5e30exc(v=vs.90).aspx


Solution:4

The Parallel members will always throw an Aggregate exception. You are catching the wrong type.
The changes you need:
//catch(MyException e)  catch (AggregateException ae)  {              foreach (var e in ae.InnerExceptions)  // note the 's' in InnerExceptions     {          //do something with 'e'     }  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »