In previous versions of MonoTouch, I used to do this to ignore unobserved exceptions:
TaskScheduler.UnobservedTaskException += delegate(object sender, UnobservedTaskExceptionEventArgs e) {
Console.WriteLine (e);
e.SetObserved ();
};
Whether it is a good practice is debatable but I'd like to know to achieve the same effect with async
/await
keywords now officially supported in Xamarin.iOS 6.4.
Here is the code I use for testing:
async void OnClick (object sender, EventArgs e)
{
await Task.Run (() => { throw new Exception (); });
}
When I run it, debugger pauses in AsyncVoidMethodBuilder
:
I read that .NET 4.5 supposedly changed the behaviour so unobserved exceptions don't crash the app—but this doesn't help if exceptions are posted to UIKit synchronisation context where I can't handle them.
Is there a way to ignore unobserved exceptions from await
in MonoTouch?
AsyncVoidMethodBuilder
. Does that mean that if you hit continue the exception is eventually swallowed by your handler? Is this a case where only the debugger halts on this exception? Also, the article you referenced includes some app.config settings for making the exception work like .NET 4. Would that be helpful? – StewUIKitSynchronizationContext
. The config settings are there to make behavior stricter so they shouldn't be of help. You raised a valid point though; in previous versions, I was able to catch the exceptions in Unobserved handler even though there were thrown on the UI thread. – Esophagus