I am working on Windows 8 (using C#) and when using the async keyword there's a scenario where i can't seem to handle exceptions well.
The scenario involves launching an async
lambda, posting it to run on the UI thread.
Exceptions that occur during the execution of the lambda code gets re-thrown on the calling thread, with no ability to catch them properly.
Example: this block of code is executed on some worker thread, and tries to schedule work on the UI thread:
await Window.Current.Dispatcher.RunAsync
(CoreDispatcherPriority.Normal
, async () =>
{
var result = await CurrentAppSimulator
.RequestProductPurchaseAsync("product, true);
}
);
If the code inside the lambda expression throws an exception, the exception is not reposted back to the body of this method. Instead, it is being thrown by the SynchronizationContext or some similar mechanism, and i can't seem to catch it.
What i'd like to have is the ability to catch this exception from this body of code (the calling code).
Is this possible?