2

This link describes how to handle exceptions in iOS

Runtime.MarshalManagedException += (object sender, MarshalManagedExceptionEventArgs args) =>
{
    Console.WriteLine ("Marshaling managed exception");
    Console.WriteLine ("    Exception: {0}", args.Exception);
    Console.WriteLine ("    Mode: {0}", args.ExceptionMode);

};
Runtime.MarshalObjectiveCException += (object sender, MarshalObjectiveCExceptionEventArgs args) =>
{
    Console.WriteLine ("Marshaling Objective-C exception");
    Console.WriteLine ("    Exception: {0}", args.Exception);
    Console.WriteLine ("    Mode: {0}", args.ExceptionMode);
};

In addition, I've seen other Xamarin samples use this in the AppDelegate

 AppDomain.CurrentDomain.UnhandledException += (sender, e) => {
 try
 {
   var exception = ((Exception)e.ExceptionObject).GetBaseException();
   Console.WriteLine("**SPORT UNHANDLED EXCEPTION**\n\n" + exception);
   exception.Track();
  }
  catch
  {
     throw;
  }
};

Question

What are the exception types (if more than Managed/Unmanaged), and how do I capture everything?

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
  • Have you tried a try catch around your main function? static void Main(string[] args) - should be in your Main.cs file. Nothing I would recommend, but it should work ;) – Lepidopteron May 02 '17 at 15:16

1 Answers1

3

It goes without saying that a 'global' exception handler should not be a replacement for correctly catching expected or anticipated exceptions from blocks of code you anticipate to see issues with (perhaps because you use the parrallel libaries, or anything with any kind of thread complexity).

'global' exception handlers (in my opinion) should be used to catch anything you've failed to spot during development and testing. Personally I write these to a log file in our applications and handle them as 'Critical Exceptions' as they will cause the application to crash. My preferred method is to assign an event to 'AppDomain.CurrentDomain.UnhandledException'.

That being said during development, testing and if during production the users of your application happen to have 'diagnostic reporting' set to on you will be able to access apples exception logs. These can be useful but bear in mind they will give you the 'native' stack trace and won't have anything specific to xamarin in them. So take them with a pinch of salt.

as to your question, assigning to 'AppDomain.CurrentDomain.UnhandledException' will capture every exception that you haven't done or forseen yourself. you don't need to know the types explicitly as the stack trace will obviously tell you what they are. it's also worth noting that you can only use that event to record information or perform very basic functions as the app will close irrespective of what you do. So use it to log as much information regarding your application as possible.

It is currently impossible to stop your application from closing if it hits the unhandledexception event.

JoeTomks
  • 3,243
  • 1
  • 18
  • 42