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

查看:334
本文介绍了在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);
        }
    }

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

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

开始故障请求:

System.Windows.ni.dll中发生了类型为'System.Net.WebException'的异常,在受管理/本地边界之前未进行处理

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

System.Windows.ni.dll中发生了类型为'System.Net.WebException'的异常,在受管理/本地边界之前未进行处理

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.

使用异步和等待时,这不是处理异常的方法吗?感谢您的投入!谢谢.

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天全站免登陆