문제

I am having problems with unhandled exceptions being thrown in the task awaiter. These exceptions are just ignored and never propagated anywhere.

Code example:

Task<bool> Execute()
{
    TaskCompletionSource<bool> tcsrc = new TaskCompletionSource<bool>();
    Task.Delay(50).ContinueWith((t) => {
        tcsrc.SetResult(true);
    });
    return tcsrc.Task;
}

async Task<bool> Test()
{
    bool result = await Execute();
    throw new Exception("test");
    return result;
}

In this example, Test() awaits on task returned by Execute() and then throws an exception. This exception is never handled anywhere and the only place I am able to catch it is Application's FirstChanceException handler. After this handler is executed, the exception gets just ignored.

The Test() itself is called via chain of async calls from some UI event handler. So I assume it should be propagated to that UI event handler and thrown there. But it never happens.

Is there something I miss?

Edit: According to FirstChanceException handler, the exception is reproduced three times before becoming ignored with the following stack trace:

at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
도움이 되었습니까?

해결책

The Task<bool> returned from your Test method will become faulted. If you await that task (or call Wait() on it) then the exception will be rethrown at that point. If you choose to ignore the faulted state of the task, then yes, you'll be ignoring the exception.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top