提出问题提出问题
 

已答复Don't get an AggregateException ...

  • 2009年6月22日 19:37ManfredSteyer 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     包含代码
    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;
                    });
                }
            }
        }
    

答案

  • 2009年6月22日 20:52Danny ShihMSFT用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    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
  • 2009年6月23日 14:38Tibor19 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    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(*.*)
  • 2009年8月3日 15:30Stephen Toub - MSFTMSFT, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    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?

全部回复

  • 2009年6月22日 20:52Danny ShihMSFT用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    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
  • 2009年6月23日 14:38Tibor19 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    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(*.*)
  • 2009年8月3日 12:19MarcZhou 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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é
    -
  • 2009年8月3日 15:30Stephen Toub - MSFTMSFT, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    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?
  • 2009年8月3日 17:35MarcZhou 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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é
    -