MSDN An example of handling an exception from a TPL is a race condition?

I am looking at an example of handling TPL exceptions from MSDN @

http://msdn.microsoft.com/en-us/library/dd537614(v=VS.100).aspx

The main form of code:

Task task1 = Task.Factory.StartNew(() => { throw new IndexOutOfRangeException(); }); try { task1.Wait(); } catch (AggregateException ae) { throw ae.Flatten(); } 

My question is: Is this a race condition? What happens if task1 throws out before it tries to execute? Am I missing something that stops it like a race?

You should not write like this:

 try { Task task1 = Task.Factory.StartNew(() => { throw new IndexOutOfRangeException(); }); task1.Wait(); } catch (AggregateException ae) { throw ae.Flatten(); } 
+4
source share
1 answer

No, the first example is absolutely right.

When an exception occurs in a Task, it ends in an AggregateException. Only when another thread joins the task, in this example, task1.Wait () is called - this is the exception associated with the join thread. Essentially, the exception is β€œstored” until it is passed back to the thread waiting for feedback.

+6
source

Source: https://habr.com/ru/post/1307183/


All Articles