每当我使用Windows.Web.Http.HttpClient类发出 HTTP 请求时,我总是像这样处理网络异常:
HttpResponseMessage response;
try
{
response = await httpClent.GetAsync(new Uri("http://www.microsoft.com"));
}
catch (Exception e)
{
// Most likely a network exception.
// Inspect e.HResult value to see what the specific error was.
}
但现在我将捕获所有异常而不仅仅是网络异常,尤其是如果 try 块包含的不仅仅是httpClient.GetAsync
调用。
各种异常 HRESULT 已经在 ABI 层自动转换为适当的托管类型(例如 E_OUTOFMEMORY 被投射到 System.OutOfMemoryException),那么为什么网络异常没有以类似的方式投射呢?