Take the 2-minute tour ×

Please consider this short console app code.

static void Main(string[] args)
        Action a = () =>
            throw new ApplicationException("Oops");

        var ar = a.BeginInvoke(null, null);
            Console.WriteLine("No message");
        catch (Exception e)

When run Visual Studio will break on the throw complaining that its unhandled. When executed outside of the debugger the code does what I expect (displays "Oops").

How do I convince Visual Studio to allow the code to execute as it would do in the real world?

share|improve this question
If the lambda was a method you could apply the DebuggerNonUserCode attribute to achieve the desired effect. –  dtb Oct 7 '11 at 9:13
@dtb: Thats well worth placing in an answer, but I'd like to see an alternative that allows me to retain the lambda. –  AnthonyWJones Oct 7 '11 at 9:21
just observation: Visual Studio complains when ar.AsyncWaitHandle.WaitOne() is called, not when a.EndInvoke(ar) which is within try. –  alpha-mouse Oct 7 '11 at 9:32
@alpha-mouse: Thats not what I'm experiencing with this exact code. Visual Studio is stopping on the throw line. –  AnthonyWJones Oct 7 '11 at 9:57
I don't, never seen it do this either and have exercised this many times. The debugger stopping on the first chance exception notification is the only explanation. What do you actually see in the Output window? –  Hans Passant Oct 7 '11 at 10:56
show 10 more comments

1 Answer

You can apply the DebuggerNonUserCode attribute to a method to hide it from the debugger.

share|improve this answer
How would you put such an attribute on an anonymous delegate? –  Ben Voigt Feb 17 '13 at 6:22
add comment

Your Answer


