14

I am having a nasty bug show up in the wild, and I can't put my finger on it. Is there a way to have a Global Try/Catch block, or a way to handle any exception that is unhanded in Monotouch.

Can I just wrap UIApplication.Main(args) in a try catch?

After the exception is caught, Id like to show a UIAlertView to display the results.

Any help?

Chris Kooken
  • 32,730
  • 15
  • 85
  • 123
  • Not sure if this is in Monotouch, but on windows you'd use http://msdn.microsoft.com/en-us/library/system.windows.application.unhandledexception(v=vs.95).aspx – Nate Jan 28 '11 at 16:22

2 Answers2

14

You can wrap UIApplication.Main (args) in a try {} catch {} but you will not be able to show a UIAlertView at that point, since we've unwound the entire stack including all the UI. What you could do is log the exception to a crash file in the Documents folder of your application bundle, and show that on the next launch or upload it to a web service.

The Exceptioneer guys were working on MonoTouch support as well, which might be worth looking into.

Geoff Norton
  • 5,056
  • 1
  • 19
  • 17
1

I have had success with using the unhandled exception handler, ie AppDomain.CurrentDomain.UnhandledException event. This seems to catch many (but not all) unhandled exceptions. It seems that crashes that are in unmanaged code can cause the app to fail without calling this event.

Geoff: would capturing this event in addition to catching exceptions falling out of UIApplication.Main make sense or would one of the methods be preferred?

Ethan
  • 486
  • 1
  • 6
  • 15
  • It doesn't really matter, but yes crashes in native code will tear you down. The app is basically corrupt at that point and there is nothing we can do to handle that gracefully. One of the monotouch community members experimented with redirecting stdout and stderr to a file using dup2(3) in release builds, so that he could upload the crashlog to himself on the next run, which is something that could be done. – Geoff Norton Feb 01 '11 at 00:57