Don't get an AggregateException ...
- Hi,
I just wanted to try out the usage of AggregateExceptions. So I tried to provoke such an exception - without success.
You can find my code at the end of this posting. What have I done wrong ?
Regards,
Manfred
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("Beam up " + person + "..." ); } } public class ExceptionSample { public void Demo() { Starship ship = new Starship(); try { Task t1 = Task.Factory.StartNew(() => ship.BeamUp("Kirk", true)); Task t2 = Task.Factory.StartNew(() => ship.BeamUp(null, false)); Task t3 = Task.Factory.StartNew(() => ship.BeamUp("Spock", true)); t1.Wait(); } catch (AggregateException ae) { ae.Handle((e) => { Console.WriteLine(" > " + e.GetType().Name); return true; }); } } }
回答
- Hi Manfred,
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).
Hope that helps,
Danny- 回答の候補に設定Stephen Toub - MSFTMSFT, モデレータ2009年6月24日 6:43
- 回答としてマークStephen Toub - MSFTMSFT, モデレータ2009年6月26日 21:21
- Hi Manfred,
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?
Best regards,
Tibi
MCT, MCDBA, MCSD.NET, MCPD 2.0(*.*), MCPD 3.5(*.*)- 回答の候補に設定Stephen Toub - MSFTMSFT, モデレータ2009年6月24日 6:43
- 回答としてマークStephen Toub - MSFTMSFT, モデレータ2009年6月26日 21:21
- Hi Marc-
Do you have "Just My Code" 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.
Is it possible this is what you're running up against?- 回答としてマークStephen Toub - MSFTMSFT, モデレータ2009年8月3日 17:53
すべての返信
- Hi Manfred,
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).
Hope that helps,
Danny- 回答の候補に設定Stephen Toub - MSFTMSFT, モデレータ2009年6月24日 6:43
- 回答としてマークStephen Toub - MSFTMSFT, モデレータ2009年6月26日 21:21
- Hi Manfred,
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?
Best regards,
Tibi
MCT, MCDBA, MCSD.NET, MCPD 2.0(*.*), MCPD 3.5(*.*)- 回答の候補に設定Stephen Toub - MSFTMSFT, モデレータ2009年6月24日 6:43
- 回答としてマークStephen Toub - MSFTMSFT, モデレータ2009年6月26日 21:21
- Hi Manfred,
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.
Maybe there is a flag within the VS 2010 configuration to change this behavior?
Best regards,
Marc André
- - Hi Marc-
Do you have "Just My Code" 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.
Is it possible this is what you're running up against?- 回答としてマークStephen Toub - MSFTMSFT, モデレータ2009年8月3日 17:53
- Hi Stephen,
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 -> Exceptions ...) and now I'm able to debug.
Thanks.
Best regards,
Marc André
-

