当调试器附加到 .NET 进程时,它(通常)会在引发未处理的异常时停止。
但是,当您使用方法时,这似乎不起作用async
。
以下代码中列出了我之前尝试过的场景:
class Program
{
static void Main()
{
// Debugger stopps correctly
Task.Run(() => SyncOp());
// Debugger doesn't stop
Task.Run(async () => SyncOp());
// Debugger doesn't stop
Task.Run((Func<Task>)AsyncTaskOp);
// Debugger stops on "Wait()" with "AggregateException"
Task.Run(() => AsyncTaskOp().Wait());
// Throws "Exceptions was unhandled by user code" on "await"
Task.Run(() => AsyncVoidOp());
Thread.Sleep(2000);
}
static void SyncOp()
{
throw new Exception("Exception in sync method");
}
async static void AsyncVoidOp()
{
await AsyncTaskOp();
}
async static Task AsyncTaskOp()
{
await Task.Delay(300);
throw new Exception("Exception in async method");
}
}
我错过了什么吗?如何使调试器中断/停止中的异常AsyncTaskOp()
?