HttpClient 中是否可能出现同步异常?

Are sync exceptions possible in HttpClient?

现在,我不是在谈论 ArgumentNullExceptionInvalidOperationException,而是或多或少只是在谈论 HttpRequestException

var responseAsync = httpClient.SendAsync(something);
try
{
    var response = await responseAsync;
}
catch(Exception)
{
}

此代码是否会抛出任何异常,或者我们是否可以安全地假设所有可能的异常只会在 await 期间发生?

"但或多或少只是关于 HttpRequestException"

MSDN:

doco 说得很清楚:

HttpRequestException The request failed due to an underlying issue such as network connectivity, DNS failure, server certificate validation or timeout.

所以答案是 “是” 如果说你的猫拔掉了网络线。

Will this code ever throw any exceptions,

是的。

or can we safely assume that all the possible exceptions can only happen during the await?

它也可以出现在下一行,因为到 SendAsync returns(不要与 Task 完成时混淆),Task已经创建,在你到达下一行之前它有很小的机会抛出。

var responseAsync = httpClient.SendAsync(something);

编辑:(来自我下面的评论) 此外,据我们所知,SendAsync 可能会在创建 Task 之前执行一些“初始检查”。在这种情况下,您需要在上面添加 try/catch