Fazer uma PerguntaFazer uma Pergunta
 

RespondidoDon't get an AggregateException ...

  • segunda-feira, 22 de junho de 2009 19:37ManfredSteyer Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Contém Código
    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;
                    });
                }
            }
        }
    

Respostas

  • segunda-feira, 22 de junho de 2009 20:52Danny ShihMSFTMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Respondido
    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
  • terça-feira, 23 de junho de 2009 14:38Tibor19 Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Respondido
    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(*.*)
  • segunda-feira, 3 de agosto de 2009 15:30Stephen Toub - MSFTMSFT, ModeradorMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Respondido
    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?

Todas as Respostas

  • segunda-feira, 22 de junho de 2009 20:52Danny ShihMSFTMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Respondido
    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
  • terça-feira, 23 de junho de 2009 14:38Tibor19 Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Respondido
    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(*.*)
  • segunda-feira, 3 de agosto de 2009 12:19MarcZhou Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    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é
    -
  • segunda-feira, 3 de agosto de 2009 15:30Stephen Toub - MSFTMSFT, ModeradorMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Respondido
    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?
  • segunda-feira, 3 de agosto de 2009 17:35MarcZhou Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    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é
    -