Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selectively preventing the debugger from stopping on 1st chance exceptions

I know I can prevent the Visual Studio debugger from stopping on certain kind of exceptions when they're thrown (via the Ctrl-Alt-E "Exceptions" dialog). But what if want to control this from code, for some specific places rather than on the all-or-none basis? For example:

try
{
    SomeMethod(token);
}
catch (OperationCancelledException)
{
    return false;
}

// ...

void SomeMethod(CancellationToken token)
{
    // ...

    // I don't want the debugger to stop on the following line
    #pragma ignore(OperationCancelledException, true)
    token.ThrowIfCancellationRequested();
    #pragma ignore(OperationCancelledException, false)
} 

I use the hypothetic #pragma ignore to illustrate what I mean, but does something like this actually exist?

UPDATE to address "Unclear what you're asking" closure vote. Try this code in the debugger: https://dotnetfiddle.net/npMk6r. Make sure all exceptions are enabled in the Ctrl-Alt-E dialog. The debugger will be stopping on the throw new OperationCanceledException("cancelled1") line upon each iteration of the loop. I don't want that to happen as it's annoying. Yet, I do want it to stop on the last throw outside the loop, throw new OperationCanceledException("cancelled2") (or anywhere else, for that matter).

like image 357
avo Avatar asked Oct 08 '14 21:10

avo


1 Answers

you try the following way:

When "Just My Code" is enabled, Visual Studio in some cases will break on the line that throws the exception and display an error message that says "exception not handled by user code." This error is benign. You can press F5 to continue from it, and see the exception-handling behavior that is demonstrated in the examples below. To prevent Visual Studio from breaking on the first error, just uncheck the "Just My Code" checkbox under Tools, Options, Debugging, General.

from here

hope this helps

like image 68
cyril.andreichuk Avatar answered Oct 05 '22 10:10

cyril.andreichuk