當調試器連接到.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()
的異常處中斷/停止?
你有沒有解決呢? –
@RichardSzalay不,不幸的不是。我想我已經習慣了這一點。 –