How get exception message that occurred inside task C++/CX?

131 Views Asked by At

How get exception message if it is occurred inside task?

For example it works fine (sync call):

IAsyncAction^ Foo() {
    throw ref new Platform::Exception(-1, "Foo exception …");
}

…
…

try {
    Foo();
}
catch (Exception^ ex) {
    OutputDebugStringW(ex->Message->Data());
}
// Output: "Foo exception …"

but it is wrong (async call):

IAsyncAction^ Foo_action() {
    return concurrency::create_async([=] {
        throw ref new Platform::Exception(-1, "Foo_action exception …");
        });
}

…
…

this->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler([this] {
        concurrency::create_task(Foo_action())
            .then([this](concurrency::task<void> previousTask) {
                try {
                    previousTask.get();
                }
                catch (Platform::Exception^ ex) {
                    OutputDebugStringW(ex->Message->Data());
                }
            });
        }));

// Output: "The text associated with this error code could not be found."

However ex->HResult retains its value.

0

There are 0 best solutions below