C# 捕获webexception时出错

C# 捕获webexception时出错,c#,windows-phone-7.1,C#,Windows Phone 7.1,我使用一个简单的webclient从web服务检索一些XML,我将其封装在一个简单的try,catch块(catching WebException)中。如下所示 try { WebClient client = new WebClient(); client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStrin

我使用一个简单的webclient从web服务检索一些XML,我将其封装在一个简单的try,catch块(catching WebException)中。如下所示

try
        {
            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            client.DownloadStringAsync(new Uri("http://ip/services"));
        }
        catch (WebException e)
        {

            Debug.WriteLine(e.Message);
        }
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.Net.WebException' occurred in System.Windows.dll
A first chance exception of type 'System.Net.WebException' occurred in System.Windows.dll
否如果我将IP地址更改为无效的地址,我当然希望它引发异常并将消息输出到调试窗口。但事实并非如此,似乎抓块甚至没有被执行。除以下内容外,不显示任何内容和调试窗口:

try
        {
            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            client.DownloadStringAsync(new Uri("http://ip/services"));
        }
        catch (WebException e)
        {

            Debug.WriteLine(e.Message);
        }
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.Net.WebException' occurred in System.Windows.dll
A first chance exception of type 'System.Net.WebException' occurred in System.Windows.dll

我的代码看起来很正确,因此我无法理解为什么没有捕获异常?

根据您对错误消息的描述,我假设实际引发的异常类型为“FileNotFoundException”

您是否尝试过捕获异常并检查类型?web异常可能是内部异常

        try
        {
            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            client.DownloadStringAsync(new Uri("http://ip/services"));
        }
        catch (Exception ex)
        {

            Debug.WriteLine(ex.GetType().FullName);
            Debug.WriteLine(ex.GetBaseException().ToString());
        }
更新:我刚刚注意到您实际调用的是一个异步方法

作为健全性检查,我建议切换到非异步方法,并检查由此产生的错误

您还可以查看本页,其中以web客户端为例介绍如何捕获异步错误


该异常永远不会从DownloadStringAsync引发。它只是不会抛出它,但DownloadString(非异步)会抛出它。我不知道这是否是一个bug,我认为异步方法不会抛出ArgumentException之外的异常,尽管文档中另有说明

您必须“捕获”DownloadStringCompletedEventHandler中的错误:

void DownloadStringCompletedEventHandler(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error != null)
    {
        Debug.WriteLine(e.Error);
        return;
    }

您几乎总是可以安全地忽略“第一次机会”异常,这些异常是在框架中捕获并相应地处理的。有关这方面的更多信息,请参阅。

是否尝试捕获一般异常?像
catch(Exception-ex)
一样,我使用Exception得到相同的结果。即使我放了一个简单的Debug.WriteLine(“test”);在catch块中,它没有被执行,这表明catch块没有被执行。ThanksAnswer更新了,因为我注意到您正在调用异步方法啊,谢谢!我不知道在异步操作时必须以不同的方式捕获异常。我没有使用你发布的方法(通过链接),而是检查了DownloadStringCompleted中的错误,这很好。谢谢你告诉我答案!