The Setting
In an async operation in a C++ WinRT component, I call a JavaScript function and wait for its completion like this:
auto callbackTask = concurrency::create_task(
dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal,
ref new Windows::UI::Core::DispatchedHandler([callback]() {
callback();
})
)
);
callbackTask.get();
I got the dispatcher using CoreWindow::GetForCurrentThread()->Dispatcher before the async operation was started.
callback is a public delegate void EachCallback(Platform::String^).
The Problem
When the called JavaScript function throws an exception (throw new Error('Some Error')), the native debugger with first-chance exceptions enabled tells me that a
Js::JavascriptExceptionObject was thrown on the callback() line. However, I can't find a way to catch it. A try/catch-block around
callback() for Platform::Object^ or std::exception doesn't catch anything. I don't know where
Js::JavascriptExceptionObject is defined, so I can't catch it directly. Even
catch (...) didn't catch anything.
So, what is the correct way and place to catch a JavaScript exception in C++? Or is it possible at all?