Skip to content

Instantly share code, notes, and snippets.

@wcoder
Last active January 29, 2018 21:10
Show Gist options
  • Save wcoder/8c15a983ab8d893bb5fdfe6850d5eae5 to your computer and use it in GitHub Desktop.
Save wcoder/8c15a983ab8d893bb5fdfe6850d5eae5 to your computer and use it in GitHub Desktop.
Exception handling on Xamarin Android
// Wire up Unhandled Expcetion handler from Android
AndroidEnvironment.UnhandledExceptionRaiser += (sender, args) =>
{
	/*
	 * When the UI Thread crashes this is the code that will be executed. There is no context at this point
	 * and no way to recover from the exception. This is where you would capture the error and log it to a 
	 * file for example. You might be able to post to a web handler, I have not tried that.
	 * 
	 * You can access the information about the exception in the args.Exception object.
	 */
};
AppDomain.CurrentDomain.UnhandledException += (s, e) =>
{
	/*
	 * When a background thread crashes this is the code that will be executed. You can
	 * recover from this.
	 * You might for example:
	 *  _CurrentActivity.RunOnUiThread(() => Toast.MakeText(_CurrentActivity, "Unhadled Exception was thrown", ToastLength.Short).Show());
	 *  
	 * or
	 * 
	 * _CurrentActivity.StartActivity(typeof(SomeClass));
	 * _CurrentActivity.Finish();
	 *
	 * It is up to the developer as to what he/she wants to do here.
	 * 
	 * If you are requiring a minimum version less than API 14, you would have to set _CurrentActivity in each time
	 * the a different activity is brought to the foreground.
	 */
};

// Wire up the unobserved task exception handler
TaskScheduler.UnobservedTaskException += (sender, args) =>
{
	// args.Exception
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment