4

I want to prevent my app from closing after an unhandled exception has been raised.

I'm doing this with Xamarin and MonoMac, but I think I could translate Objective-C answers to C#.

When an Exception happens and it's not caught anywhere, I register the event of unhandled exceptions:

AppDomain.CurrentDomain.UnhandledException += HandleUnhandledException;

And log it:

static void HandleUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    //Log the exception...
}

But then, when an exception happens, the HandleUnhandledException is executed and the event loop is exited:

NSApplication.Main(args);

So my app ends. How can I avoid this? Where should I place a try-catch block?

Luis
  • 2,833
  • 3
  • 27
  • 41
  • 1
    HandleUnhandledException is always called if you add it in a way you described above.It is called before crash as you have not handled some exception in your code.you can not avoid crashing once you come inside that handler.please put your code if you want to know where to put try catch. – vITs May 28 '14 at 12:44
  • where I should add handler in main.cs or inside AppDelegate? – Nininea Oct 25 '16 at 20:27

2 Answers2

3

This is what the xamarin developer center has to say:

"You can add an event handler to the AppDomain.CurrentDomain.UnhandledException event, and you will get notified when unhandled managed exceptions occur (note that the app will exit as soon as you return from your event handler no matter what you do so pretty much the only purpose would be for you to collect diagnostic information. Also it will only catch managed exceptions, not native crashes, which is another cause of exiting apps)."

So, apparently, yo can add a global try catch block but that will just be to tell you about the managed exceptions and the app will exit by the end of the try catch. So it won't serve your purpose fully.

Gaurav Deochakke
  • 2,265
  • 2
  • 21
  • 26
  • Thanks Gaurav; the question is: where should I put the try-catch block? In main.cs? When the NSApplication.Main(args); has exited there's little to do to return it to work... – Luis May 28 '14 at 12:07
  • "I want to prevent my app from closing after an unhandled exception has been raised." I don't get this line. You come in the handler of the Unhandled exception ONLY when your app has crashed and you are given a final chance to Log anything if you want. I don't see any possible way which can avoid a crash after you are in this handler. Your app will terminate obviously after coming in this handler of unhandled exceptions. You must explicitly use try catch blocks inside your code everywhere where, you think, your code can throw Exceptions. – Gaurav Deochakke May 28 '14 at 12:37
  • Alright, I was looking for something like Windows, where you can enclose the code in the entry point (the main method) in a try-catch block and handle the unexpected (mostly bugs) gracefully, avoiding the application to be closed. So, I see this is not possible in Mac, right? – Luis May 28 '14 at 12:40
  • Yes right. I am a windows phone developer. In wp8 you have this handler placed Outside your own written code into app.xaml.cs where you come only when you have lost control of your application and you just have to write logs. This handler is by default present in that file. So even there you don't have control over the app anymore. So yeah you are right about that. Hope this helped you. Do upvote or accept if you think it helped. Thanks and Cheers. – Gaurav Deochakke May 28 '14 at 12:44
  • I'm working on a desktop application for Mac OSX, so the aim is rather different here. I understand that (maybe) the best decision when a phone app fails is stop, but it's not quite like that in desktop applications, not always at least. – Luis May 28 '14 at 13:34
  • I just specified my platform because it was more relevent. I also work on windows 8, WPF where also the same is the pattern. – Gaurav Deochakke May 28 '14 at 16:24
0

Ok, i know this is a little bit old but i was struggle with this too for many months, since my app crashed without any stack trace... but! ... i figure it out after many try and catch here and there, that you can put in the constructor of your AppDelegate.cs class the following:

            AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs e) => {
                Exception GivenException = (Exception) e.ExceptionObject;
                Console.WriteLine( GivenException.Message);
                Console.WriteLine( GivenException.StackTrace);
                Console.WriteLine("Runtime terminating: {0}", e.IsTerminating);
            };

This will not avoid the app for terminate but at least you will know exactly where to look and fix the issues.

  • Right, but as I stated in the question this logs the problem but doesn't aviod the crash. Anyway, I realized that what I wanted to do was not possible and... non-sense, either. The best thing you can do in these cases is just log the exception. – Luis Mar 14 '15 at 11:41