질문하기질문하기
 

답변됨Don't get an AggregateException ...

  • 2009년 6월 22일 월요일 오후 7: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일 월요일 오후 8: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일 화요일 오후 2: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일 월요일 오후 3: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일 월요일 오후 8: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일 화요일 오후 2: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일 월요일 오후 3: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일 월요일 오후 5: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é
    -