0

I'm having some trouble with an exception I am getting from some async code. I don't want to block my thread while the

private async Task ThrowSomeExceptionAsync()
{
    //Some long running process would go here...

    throw new Exception();
}

This is the method I want to call, and to catch the exception. I am using the Application.UnhandledException event to catch my exceptions, but in this case it's not being caught.

I tried waiting for the Exception property on the returned Task object to be populated, but that blocks while the task is being completed.

Using the ContinueWith method prevented blocking, but any exceptions thrown from the delegate weren't caught by the UndhandledException event.

How can I execute this code in an async fashion, yet still use my UnhandledException event for error handling? Am I missing some fundamental concept about that event?

4

1 回答 1

1

The reason the exception isn't being sent to the UnhandledException event is that the event only covers exceptions raised in the XAML framework. The delegate that's throwing the exception exists outside of the XAML framework, and doesn't pass it along so that the event can be raised. The solutions appears to be in the form of a static class that will handle exceptions, and call that from both the delegate and within the UnhandledException event.

于 2012-09-12T19:20:25.880 回答