I am trying to implement a useful general exception handler for my MonoTouch code.
If I attach a handler to AppDomain.CurrentDomain.UnhandledException, there is no stack trace present at all, i.e. the .StackTrace property is null or empty.
If I wrap my UIApplication.Main(args) call in try {} catch {}, the stack trace doesn’t contain any useful information:
at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00000] in <filename unknown>:0
at MonoTouch.UIKit.UIApplication.Main (System.String[] args) [0x00000] in <filename unknown>:0
at MyNamespace.MyProduct.MyProject.Application.Main (System.String[] args) [0x00000] in <filename unknown>:0
i.e. it doesn’t go any deeper than the Main() method where I caught the exception.
Any ideas how I can get some more useful information in the stack trace at all, or is it all optimised away completely by the AOT compilation? (The stack trace is as expected in debug mode.)
You won’t be able to get line numbers or file names in release code. That information is stripped out. However, you should still be able to get the full stack trace into your code to at least see the class/call hierarchy.
I’m guessing you must be looking into this after the fact though, seeing as you can’t debug or catch running exceptions on a release build. What I tend to do is log my exceptions to a log file to check later on.
This process starts with the following code:
You can also create StackTrace objects with an exception too. I would start playing around there. Keep in mind that in many situations, you can’t get a stack trace if the error occurs deep within the iOS portion of things as Mono isn’t exposed to that data. An exception or crash that occurs that only goes up to the main method usually indicates a lower level issue.