我正在使用一个简单的Web客户端从Web服务检索一些XML,我有这个封装在一个简单的尝试,catch块(捕捉WebException)。如下所示;错误捕捉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)' – NaveenBhat
我使用异常获得相同的结果。谢谢 – Nathan