【发布时间】:2015-05-09 12:32:22
【问题描述】:
我打算使用以下代码下载文件。 WIFI可用时有效;但是当没有 Wifi 时,我希望捕捉到previousTask.get() 中引发的异常。不幸的是,我的代码中的catch 似乎没有捕捉到异常。顺便说一下,HRESULT:0x80072F30 The text associated with this error code could not be found. 是个例外。我是否遗漏了异常无法捕获之类的内容?
auto httpClient = ref new HttpClient();
auto get_operation = httpClient->GetAsync(ref new Uri(url), HttpCompletionOption::ResponseContentRead);
get_operation->Progress = progressHandler;
auto response = create_task(get_operation).then([](task<HttpResponseMessage^> previousTask)
{
try
{
return previousTask.get();
}
catch (Exception^ ex)
{
// Some how this does not catch
OutputDebugString(("Exception: " + ex->Message)->Data());
return (HttpResponseMessage^)nullptr;
}
}).get();
// At this point, I expect either a fully read response or response=nullptr
// Code to write to file is omitted
编辑:~~我测试了微软官方的HttpClient sample,显然使用了类似的代码。显然,当没有网络连接时,该应用程序会发生同样的崩溃。这样就确定了缺陷在操作系统端,没有办法解决。~~
编辑:事实证明,我认为异常没有被捕获,因为 Visual Studio 会弹出一个对话框,我认为这意味着实际上异常会导致应用程序崩溃,即当它不是通过 VS 启动时.我仔细阅读了弹出消息,并意识到除非配置为不这样做,否则 VS 会提示每个抛出的异常;按对话框上的 [Continue] 按钮转到 catch 子句。从开始菜单启动应用程序没有问题。
【问题讨论】:
-
这是真机还是模拟器? WiFi 关闭或 WiFi 开启时网络中其他地方出现问题?
-
@kiewic 这是在我的 Windows 8.1 笔记本电脑上测试的,而不是在模拟器上。
标签: windows-phone-8.1 c++-cx windows-rt