【发布时间】:2015-07-02 18:10:20
【问题描述】:
我正在使用一个简单的 web 客户端从 web 服务中检索一些 XML,我将它封装在一个简单的 try, catch 块中(捕获 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);
}
不,如果我将 IP 地址更改为无效的,我预计它会引发异常并将消息输出到调试窗口。但它没有,似乎 catch 块甚至没有被执行。除了以下之外,什么都没有出现,调试窗口也没有;
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
我的代码在我看来是正确的,所以我不明白为什么没有捕获异常?
【问题讨论】:
-
您是否尝试捕获一般异常?喜欢
catch(Exception ex) -
我使用异常得到了同样的结果。谢谢
标签: c# windows-phone-7.1