c# - How to ignore unobserved exceptions with async/await in MonoTouch? -
in previous versions of monotouch, used ignore unobserved exceptions:
taskscheduler.unobservedtaskexception += delegate(object sender, unobservedtaskexceptioneventargs e) { console.writeline (e); e.setobserved (); };
whether practice debatable i'd know achieve same effect async
/await
keywords now officially supported in xamarin.ios 6.4.
here code use testing:
async void onclick (object sender, eventargs e) { await task.run (() => { throw new exception (); }); }
when run it, debugger pauses in asyncvoidmethodbuilder
:
i read .net 4.5 supposedly changed behaviour unobserved exceptions don't crash app—but doesn't if exceptions posted uikit synchronisation context can't handle them.
is there way ignore unobserved exceptions await
in monotouch?
this correct behavior of async void
methods: supposed raise exception on synchronizationcontext
active @ time async void
method started.
the change mentioned in .net 4.5 dealing unobserved task exceptions, , not apply async void
methods.
in (microsoft) .net world, different synchronizationcontext
implementations have different top-level error handling. wpf, winforms, , asp.net have different ways of handling error, part of application
type.
i looked through mono's uikit api - though i'm not regular mono user - , couldn't find top-level error handling in uiapplication
, , uikitsynchronizationcontext
looks it's not public (or @ least not documented).
another way of looking @ problem: exception handling behavior async void
methods designed event handlers have (for more info, see my msdn article). can answer question question: in uikit, how handle exception?
void onclick (object sender, eventargs e) { throw new exception(); }
you handle async void
exception in same way.
alternatively, if want keep using unobservedtaskexception
, can not observe task exception (in async void
code, task.run
returns task gets exception, , you're observing using await
):
void onclick (object sender, eventargs e) { task.run(() => { throw new exception(); }); }
however, recommend using async void
event handlers , (eventually) await
ing tasks. ensure aren't getting "silent errors" (ignored task exceptions) program stops working correctly , don't know why.
Comments
Post a Comment