在 async/await 中正确处理 HttpClient 异常 [英] Properly handling HttpClient exceptions within async / await

查看:39
本文介绍了在 async/await 中正确处理 HttpClient 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人能就我在使用 HttpClient 处理异步/等待异常时遇到的问题给我一点启发.我已经写了一些代码来说明,它正在 Windows Phone 8 设备和模拟器上执行:

I was hoping somebody could enlighten me a little bit on an issue I am facing in regards to async/await exception handling with HttpClient. I have written some code to illustrate, and it is being excecuted on both a Windows Phone 8 device and the emulator:

    private async void SearchButton_Click(object sender, EventArgs e)
    {
        try
        {
            HttpClient client = new HttpClient();
            System.Diagnostics.Debug.WriteLine("BEGIN FAULTY REQUEST:");
            string response = await client.GetStringAsync("http://www.ajshdgasjhdgajdhgasjhdgasjdhgasjdhgas.tk/");
            System.Diagnostics.Debug.WriteLine("SUCCESS:");
            System.Diagnostics.Debug.WriteLine(response);
        }
        catch (Exception exception)
        {
            System.Diagnostics.Debug.WriteLine("CAUGHT EXCEPTION:");
            System.Diagnostics.Debug.WriteLine(exception);
        }
    }

点击调用此函数的按钮,会在调试器控制台中产生以下输出,最有趣的是粗体:

Tapping the button that invokes this function, produces the following output in the debugger console, the most interesting being the ones in bold:

开始错误请求:

System.Net.WebException"类型的异常发生在 System.Windows.ni.dll 中,并且在托管/本机边界之前未被处理

An exception of type 'System.Net.WebException' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary

System.Net.WebException"类型的异常发生在 System.Windows.ni.dll 中,并且在托管/本机边界之前未被处理

An exception of type 'System.Net.WebException' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary

在 mscorlib.ni.dll 中发生类型为System.Net.Http.HttpRequestException"的第一次机会异常

A first chance exception of type 'System.Net.Http.HttpRequestException' occurred in mscorlib.ni.dll

在 mscorlib.ni.dll 中发生了System.Net.Http.HttpRequestException"类型的异常,并且没有在托管/本机边界之前处理

An exception of type 'System.Net.Http.HttpRequestException' occurred in mscorlib.ni.dll and wasn't handled before a managed/native boundary

引起注意的异常:

(并在此处打印出 HttpRequestException)

(and here it prints out the HttpRequestException)

当然,我期待在这种情况下出现错误,因为我调用的 URL 是无意义的.我在这里不明白的是,为什么调试器报告异常未被处理,同时输出报告异常被捕获.此外,在打印输出时,应用程序的 UI 端响应速度变慢,这表明可能有问题.

Of course I am expecting an error in this case since the URL I am calling is nonsense. What I am not understanding here, is why the debugger reports that the exceptions are not handled, when the output simultaneously reports that the exception is caught. Also, the UI side of the app becomes much less responsive while the output is being printed, indicating that something is probably amiss.

这不是在使用 async 和 await 时处理异常的方法吗?我感谢任何输入!谢谢.

Is this not the way to handle exceptions when working with async and await? I appreciate any input! Thanks.

推荐答案

这是调试器的产物.它确定一个异常是未捕获的",因为它还没有被捕获.在这种情况下,这是预期的行为.

This is an artifact of the debugger. It's determining that an exception is "uncaught" because it's not caught yet. In this case this is expected behavior.

您正在正确处理异常.

这篇关于在 async/await 中正确处理 HttpClient 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆