Don't get an AggregateException ...Hi,<br/><br/>I just wanted to try out the usage of AggregateExceptions. So I tried to provoke such an exception - without success.<br/>You can find my code at the end of this posting. What have I done wrong ?<br/><br/>Regards,<br/>Manfred<br/><br/> <pre lang="x-c#"> class TransporterDamagedException : ApplicationException {} class CannotLocatePersonException : ApplicationException { } class Starship { public void BeamUp(String person, bool urgent) { if (urgent) throw new TransporterDamagedException(); if (person == null) throw new CannotLocatePersonException(); Console.WriteLine(&quot;Beam up &quot; + person + &quot;...&quot; ); } } public class ExceptionSample { public void Demo() { Starship ship = new Starship(); try { Task t1 = Task.Factory.StartNew(() =&gt; ship.BeamUp(&quot;Kirk&quot;, true)); Task t2 = Task.Factory.StartNew(() =&gt; ship.BeamUp(null, false)); Task t3 = Task.Factory.StartNew(() =&gt; ship.BeamUp(&quot;Spock&quot;, true)); t1.Wait(); } catch (AggregateException ae) { ae.Handle((e) =&gt; { Console.WriteLine(&quot; &gt; &quot; + e.GetType().Name); return true; }); } } }</pre> <br/>© 2009 Microsoft Corporation. All rights reserved.Mon, 03 Aug 2009 17:53:52 Zb433e6c6-30cd-40fa-ac31-f5e8958f847dhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/b433e6c6-30cd-40fa-ac31-f5e8958f847d#b433e6c6-30cd-40fa-ac31-f5e8958f847dhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/b433e6c6-30cd-40fa-ac31-f5e8958f847d#b433e6c6-30cd-40fa-ac31-f5e8958f847dManfredSteyerhttp://social.msdn.microsoft.com/Profile/en-US/?user=ManfredSteyerDon't get an AggregateException ...Hi,<br/><br/>I just wanted to try out the usage of AggregateExceptions. So I tried to provoke such an exception - without success.<br/>You can find my code at the end of this posting. What have I done wrong ?<br/><br/>Regards,<br/>Manfred<br/><br/> <pre lang="x-c#"> class TransporterDamagedException : ApplicationException {} class CannotLocatePersonException : ApplicationException { } class Starship { public void BeamUp(String person, bool urgent) { if (urgent) throw new TransporterDamagedException(); if (person == null) throw new CannotLocatePersonException(); Console.WriteLine(&quot;Beam up &quot; + person + &quot;...&quot; ); } } public class ExceptionSample { public void Demo() { Starship ship = new Starship(); try { Task t1 = Task.Factory.StartNew(() =&gt; ship.BeamUp(&quot;Kirk&quot;, true)); Task t2 = Task.Factory.StartNew(() =&gt; ship.BeamUp(null, false)); Task t3 = Task.Factory.StartNew(() =&gt; ship.BeamUp(&quot;Spock&quot;, true)); t1.Wait(); } catch (AggregateException ae) { ae.Handle((e) =&gt; { Console.WriteLine(&quot; &gt; &quot; + e.GetType().Name); return true; }); } } }</pre> <br/>Mon, 22 Jun 2009 19:37:36 Z2009-06-22T19:37:36Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/b433e6c6-30cd-40fa-ac31-f5e8958f847d#c6244194-6e2e-41b4-a1cb-0f2a176f533ahttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/b433e6c6-30cd-40fa-ac31-f5e8958f847d#c6244194-6e2e-41b4-a1cb-0f2a176f533aDanny Shihhttp://social.msdn.microsoft.com/Profile/en-US/?user=Danny%20ShihDon't get an AggregateException ...Hi Manfred,<br/><br/>Do you not get an AggregateException with this code?  I think you would, but it would be unhandled since you only Wait on t1 (the exceptions for t2 and t3 are unobserved).  Try replacing t1.Wait() with Task.WaitAll(t1, t2, t3).<br/><br/>Hope that helps,<br/>DannyMon, 22 Jun 2009 20:52:31 Z2009-06-22T20:52:31Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/b433e6c6-30cd-40fa-ac31-f5e8958f847d#4030bc06-169a-4fbb-8675-4e084bb1d0fbhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/b433e6c6-30cd-40fa-ac31-f5e8958f847d#4030bc06-169a-4fbb-8675-4e084bb1d0fbTibor19http://social.msdn.microsoft.com/Profile/en-US/?user=Tibor19Don't get an AggregateException ...Hi Manfred,<br/><br/>The problem is that your Task is too quick, so when you call wait, the application already is complaining about an unhandled exception. Have you tried to use a SpinWait, or some other kind of delay, to see how it reacts?<br/><br/>Best regards,<br/>Tibi <hr class="sig">MCT, MCDBA, MCSD.NET, MCPD 2.0(*.*), MCPD 3.5(*.*)Tue, 23 Jun 2009 14:38:35 Z2009-06-23T14:38:35Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/b433e6c6-30cd-40fa-ac31-f5e8958f847d#cda6c11c-4951-489e-8059-3db7dcf79b8bhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/b433e6c6-30cd-40fa-ac31-f5e8958f847d#cda6c11c-4951-489e-8059-3db7dcf79b8bMarcZhouhttp://social.msdn.microsoft.com/Profile/en-US/?user=MarcZhouDon't get an AggregateException ...Hi Manfred,<br/><br/>I encountered the same issue as you. I've similar code and the AggregateException is not thrown when I start my example from Visual Studio 2010. But when I build the solution (Release Mode) and start the created *.exe directly, everything is working as expected.<br/>Maybe there is a flag within the VS 2010 configuration to change this behavior?<br/><br/>Best regards,<br/>Marc André<hr class="sig">-Mon, 03 Aug 2009 12:19:21 Z2009-08-03T12:19:21Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/b433e6c6-30cd-40fa-ac31-f5e8958f847d#6422ae22-6860-465e-a297-2659410828b9http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/b433e6c6-30cd-40fa-ac31-f5e8958f847d#6422ae22-6860-465e-a297-2659410828b9Stephen Toub - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Stephen%20Toub%20-%20MSFTDon't get an AggregateException ...Hi Marc-<br/><br/>Do you have &quot;Just My Code&quot; enabled (it's the default)?  If so, this will cause Visual Studio to break in automatically when an exception leaves your code (e.g. a delegate passed to a Task) and enters non-user code (e.g. the Task's internal implementation).  This allows you to see exactly when an exception is going unhandled, but in doing so, it may appear that you're not getting an AggregateException, because VS is breaking in before TPL has a chance to wrap.  If you hit F5, however, the debugger will allow execution to continue, and the implementation will continue on by wrapping and throwing the aggregate and so forth.<br/><br/>Is it possible this is what you're running up against?Mon, 03 Aug 2009 15:30:41 Z2009-08-03T15:30:41Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/b433e6c6-30cd-40fa-ac31-f5e8958f847d#eb322578-7eb4-4edd-906e-af5a8509486fhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/b433e6c6-30cd-40fa-ac31-f5e8958f847d#eb322578-7eb4-4edd-906e-af5a8509486fMarcZhouhttp://social.msdn.microsoft.com/Profile/en-US/?user=MarcZhouDon't get an AggregateException ...Hi Stephen,<br/><br/>thanks for the hint. Now it is working, also in debug mode. I've deactivated the User-unhandled flag for the Common Language Runtime Exceptions (Debug -&gt; Exceptions ...) and now I'm able to debug.<br/><br/>Thanks.<br/><br/>Best regards,<br/>Marc André<hr class="sig">-Mon, 03 Aug 2009 17:35:50 Z2009-08-03T17:35:50Z