tags:

views:

61

answers:

4

I'm writing some fairly complex C# code. I'm finding that my code is throwing an exception (noted in the Output window) but the debugger isn't breaking in. I do have Exceptions sets to break on user-unhandled CLR exceptions.

Since the debugger isn't breaking in, I assume that there must be a try{} somewhere in the call stack.

Problem is, I can't find it.

How do I find the exception handler for this line of code that's throwing an exception?

+2  A: 

Check the trace so you can see from where exception is thrown.

Incognito
+3  A: 

Open the exceptions window in Visual Studio. Expand the appropriate tree to find the exception that is being thrown and check the "Thrown" checkbox. The next time that exception is thrown in debug mode (vice uncaught) the debugger will immediately break.

confusedGeek
+2  A: 

Set exceptions to break on throw Debug -> Exceptions to the 'Exceptions' dialog and then 'Common Language Runtime Exceptions' check 'Thrown' checkbox.

This will stop as soon as ANY exception is thrown.

You may find a surprising number of exceptions are thrown as part of your 'normal' code execution (things like socket exceptions or Parse exceptions when your UI is databinding) in which case, switch off the 'break on throw' checkbox and place a breakpoint near your offending code. Debug to the breakpoint and then switch break on throw back on and continue the debugger.

Dr Herbie
A: 

You can try to catch application level excpetion. Just add to your Main method following code:

Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

And handle all expections in following method:

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
    {
        //Log Excpetion or place breakpoint
    }

If you place a breakpoint inside the handler you will be able to see StackTrace of an exception.

Hope this helps.

Bashir Magomedov
That won't help, most likely. His app isn't crashing, which those are good for.
Will
Yeah, you might be right :)
Bashir Magomedov