Struggling to understand Xamarin exception handling

后端 未结 1 1807
北海茫月
北海茫月 2020-12-31 09:49

I have been crawling the Internet for quite a long time in hope of a solution, and I\'ve come across a number of answers, but none of these seem to achieve what I want.

相关标签:
1条回答
  • 2020-12-31 10:37

    There is one important thing you have to understand about the nature of an Unhandled exception in Android, there isn't one.... in Android framework which uses Java it's an Uncaught exception which means you can't "handle" it or recover from it like you maybe would in a .Net environment. Xamarin(Mono) internally "handles" those uncaught exceptions by surrounding literally everything with try-catch and raising the Unhandled event but that is besides the point. It is also discouraged to interact with the UI as well for various reasons. Theoretically there are several "workarounds" for displaying a dialog to the user or restarting the app, none of which I'd recommend on doing. Instead you should surround sensitive areas with try-catch clauses to handle expected exceptions, as for the unexpected one's just use an exception reporting component and update your app after analyzing the reported exceptions.

    Also, I would move the event subscription to the Application class but that is a personal preference. Like so:

    public class YourAppClass : Application
    {
        public override void OnCreate()
        {
            AndroidEnvironment.UnhandledExceptionRaiser += HandleAndroidException;
        }
    }
    
    0 讨论(0)
提交回复
热议问题