Debugger not breaking/stopping for exceptions in async method

天涯浪子 提交于 2019-11-28 16:57:36

Under the Debug menu, select Exceptions.... In the Exceptions dialog, next to the Common Language Runtime Exceptions line check the Thrown box.

Jimbob

I would like to hear if anyone found out how to get around this issue? Perhaps a setting in latest visual studio...?

A nasty but workable solution (in my case) was to throw my own custom Exception and then modify Stephen Cleary's answer:

Under the Debug menu, select Exceptions (You can use this Keyboard shortcut Control + Alt + E)... In the Exceptions dialog, next to the Common Language Runtime Exceptions line check the Thrown box.

to be more specific i.e., add your custom Exception into the list, and then tick its "Thrown" box.

E.g:

async static Task AsyncTaskOp()
{
    await Task.Delay(300);
    throw new MyCustomException("Exception in async method");
}
Craig

I have wrapped the anonymous delegate in a try/catch inside the Task.Run(() =>.

Task.Run(() => 
{
     try
     {
          SyncOp());
     }
     catch (Exception ex)
     {
          throw;  // <--- Put your debugger break point here. 
                  // You can also add the exception to a common collection of exceptions found inside the threads so you can weed through them for logging
     }

});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!