Suppose I have the following code which returns an HttpWebResponse given an HttpWebRequest:
HttpWebRequest request = ...;
Task<WebResponse> task = Task<WebResponse>.Factory
.FromAsync(
request.BeginGetResponse
, request.EndGetResponse
, null
, TaskCreationOptions.None
);
if (task.Wait(TimeSpan.FromSeconds(200)))
{
// Ok, request completed, return the response
return task.Result;
}
else
{
throw new WebException("GetResponse timed out", new TimeoutException());
// is it possible that we end with an unobserved exception? I.e.,
// what if BeginGetResponse/EndGetResponse throws
// immediately after task.Wait has returned false?
}
What happens if the web request fails immediately after the task has timed out and has returned false? Will the task consider it an "unobserved" exception, to be thrown when its finalizer runs?
Note that the caller of this code is prepared to handle any exceptions which might be thrown before the task finishes.