を使用して、非同期タスクによってスローされる可能性のある例外をラップしようとしていますContinueWith()
。継続アクションからスローしただけでは動作しているように見えますが、デバッガーは例外が処理されていないと主張しています。何か間違ったことをしていますか、それとも Visual Studio の問題ですか? これを行うためのよりクリーンな方法、または最終的に処理された例外で停止するデバッガーを回避する方法はありますか?
以下のテストは成功し、「期待どおりにラップされた例外がキャッチされました」と出力されますが、デバッグすると、throw new CustomException
「ユーザーコードによって処理されていない」という行が表示されます。
var task = DoWorkAsync().ContinueWith(t => {
throw new CustomException("Wrapped", t.Exception.InnerException); // Debugger reports this unhandled
}, TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously);
try {
task.Wait();
Assert.Fail("Expected work to fail");
} catch (AggregateException ag) {
if (!(ag.InnerException is CustomException))
throw;
}
Console.WriteLine("Caught wrapped exception as expected");