Stop Visual Studio from breaking on exception in Tasks

前端 未结 3 523
闹比i
闹比i 2020-12-06 00:34

I have the following code:

Task load = Task.Factory.StartNew(() => {//Some Stuff Which Throws an Exception});

try
{
    load.Wait();
}
catch (AggregateExc         


        
相关标签:
3条回答
  • 2020-12-06 01:08

    To turn off stop on exceptions press " Ctrl + Alt + E ". This will open the Exceptions window . Untick "Common Language Runtime Exceptions - Thrown".

    0 讨论(0)
  • 2020-12-06 01:15

    Ok I found out how to do it. The answer is right here in the note section

    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 and see the exception-handling behavior that is demonstrated in these examples. To prevent Visual Studio from breaking on the first error, just uncheck the Enable Just My Code checkbox under Tools, Options, Debugging, General.

    0 讨论(0)
  • 2020-12-06 01:21

    From the point of view of VS, there really isn't any difference between the exception being thrown from within a delegate passed to a Task from any other exception.

    There is no way to solve this in the general case.

    However, the one thing that you could do is leverage the fact that when the exception is re-thrown, it's wrapped in an AggregateException. You could break when an AggregateException is thrown but not other exceptions.

    You can go to Debug -> Exceptions, unselect all CLR exceptions, but then re-enable aggregate exceptions:

    enter image description here

    It will now not pause in the Task body but will pause the debugger when you call Wait.

    The unfortunate side effect is that you'll now no longer pause for any other exceptions anywhere else in your program, even if they're not in a delegate passed to a Task.

    0 讨论(0)
提交回复
热议问题