Search code examples
windows-phone-8.1c++-cxwindows-rt

Asynchronous download with HttpClient: The text associated with this error code could not be found


I intend to use the following code to download a file. It works when WIFI is available; but when there is no Wifi, I expect to catch the exception raised in the previousTask.get(). Unfortunately, catch in my code doesn't seem to catch the exception. The exception is HRESULT:0x80072F30 The text associated with this error code could not be found., by the way. Am I missing something like the exception is uncaughtable?

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

EDIT: ~~I tested the official Microsoft's HttpClient sample which apparently use similar code. Apparently, the same crash occurs in that app when there is no network connection. This sort of confirms that the defect is in the OS side and there's nothing one can do about it.~~

EDIT: It turns out that I thought the exception was not caught because Visual Studio pops up a dialog and I assume that means in reality the exception crashes the app i.e. when it is not launched via VS. I read the pop up message carefully and realize that VS prompts on every Exception thrown unless configured not to do so; pressing [Continue] button on the dialog goes to the catch clause. Launching app from Start menu poses no problem.


Solution

  • If this code is called from the UI thread then remove the get() call from the last line of this code. You can't do that in a UI thread.

    Otherwise your code works fine for me with Airplane mode turned on; as expected I catch the exception in the handler. The exception has an HResult of 0x80072f30, which is documented on the MSDN page as ERROR_WINHTTP_NO_CM_CONNECTION