Nat*_*han 5 c# windows-phone-7.1
我正在使用一个简单的webclient从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);
}
Run Code Online (Sandbox Code Playgroud)
不,如果我将IP地址更改为无效的IP地址,我希望它抛出异常并将消息输出到调试窗口.但它没有,似乎捕获块甚至没有被执行.没有出现任何内容,调试窗口与以下内容不同;
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
Run Code Online (Sandbox Code Playgroud)
我的代码对我来说是正确的,所以我无法理解为什么异常没有被捕获?
根据您对错误消息的描述,我假设抛出的实际异常是"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());
}
Run Code Online (Sandbox Code Playgroud)
更新:我刚刚注意到你实际调用的是一个异步方法.
作为一个完整性检查,我建议交换非异步方法并检查由此产生的错误.
WebClient.DownloadString方法(Uri)
您也可以从查看此页面中受益,该页面使用Web客户端作为示例逐步捕获异步错误.
| 归档时间: |
|
| 查看次数: |
13744 次 |
| 最近记录: |