Cообщество разработчиков на платформе Microsoft >
Форумы
>
Parallel Extensions to the .NET Framework
>
Don't get an AggregateException ...
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, Модератор24 июня 2009 г. 6:43
- Помечено в качестве ответаStephen Toub - MSFTMSFT, Модератор26 июня 2009 г. 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, Модератор24 июня 2009 г. 6:43
- Помечено в качестве ответаStephen Toub - MSFTMSFT, Модератор26 июня 2009 г. 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, Модератор3 августа 2009 г. 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, Модератор24 июня 2009 г. 6:43
- Помечено в качестве ответаStephen Toub - MSFTMSFT, Модератор26 июня 2009 г. 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, Модератор24 июня 2009 г. 6:43
- Помечено в качестве ответаStephen Toub - MSFTMSFT, Модератор26 июня 2009 г. 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, Модератор3 августа 2009 г. 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é
-

