Right now, I'm not talking about ArgumentNullException
or InvalidOperationException
, but more or less only about HttpRequestException
.
var responseAsync = httpClient.SendAsync(something);
try
{
var response = await responseAsync;
}
catch(Exception)
{
}
Will this code ever throw any exceptions, or can we safely assume that all the possible exceptions can only happen during the await
?
"but more or less only about HttpRequestException"
MSDN:
The doco makes it quite clear:
HttpRequestException The request failed due to an underlying issue such as network connectivity, DNS failure, server certificate validation or timeout.
So the answer would be "yes" if say your cat pulled out the network lead for example.
Will this code ever throw any exceptions,
Yes.
or can we safely assume that all the possible exceptions can only happen during the await?
It can occur on the following line too because by the time SendAsync
returns (not to be confused with when the Task
is complete), the Task
has already been created and there is a tiny chance of it throwing before you get to the next line.
var responseAsync = httpClient.SendAsync(something);
EDIT: (from my comment below)
Also, for all we know, SendAsync
might perform some "initial check" prior to the Task
being created. In which case you'll need a try/catch
on the above.