我正在使用一个简单的Web客户端从Web服务中检索一些XML,我将它包含在一个简单的try-catch块中(捕获WebException)。如下所示:
如果我将IP地址更改为无效的地址,我希望它会抛出异常并输出消息到调试窗口。但它没有,似乎连catch块都没有被执行。除了以下内容以外,调试窗口中什么也没有显示;
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)
。 - NaveenBhat