I know that catching all exceptions in an app is generally bad, but in my WPF app I do:
Application.Current.DispatcherUnhandledException += (s, e) => {
ReportException(e.Exception, false);
e.Handled = true;
};
This is mostly useful to prevent minor features (like drag&drop) and async code from crashing the app - I log & display the info to the user.
I want to do the same thing in Xamarin.Mac, but there doesn't seem to be an equivalent. I tried:
AppDomain.CurrentDomain.UnhandledException += (sender, args) => Log(args.Exception);
TaskScheduler.UnobservedTaskException += (sender, args) =>
{
args.SetObserved();
Log(args.Exception);
};
Runtime.MarshalManagedException += (sender, args) => Log(args.Exception);
Runtime.MarshalObjectiveCException += (sender, args) => Log(args.Exception);
But when crashing in an async void
method, TaskScheduler.UnobservedTaskException
, MarshalManagedException
and MarshalObjectiveCException
are not called,
and AppDomain.Current.UnhandledException
's e.IsTerminating
is get-only, so I can't prevent the app from exiting.
For Android there's AndroidEnvironment.UnhandledExceptionRaiser += AndroidEnvironmentOnUnhandledException;
But for Mac there doesn't seem to be a way to cancel the app going down?