Struggling to understand Xamarin exception handling

ぐ巨炮叔叔 提交于 2020-01-01 02:30:27

问题


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.

I'm trying to handle exceptions without causing the app to crash. Rather than the app simply exiting, I would rather capture the exception, present the user with a more user-friendly error (perhaps a messagebox warning) and allow them to continue operation in the app.

Is it possible to stop the app from bailing out?

The way I'm currently attempting to catch this is like the following:

public class Login : Activity
{
    int count = 1;
    Session mySession;

   protected override void OnCreate(Bundle bundle)
    {
        AndroidEnvironment.UnhandledExceptionRaiser += HandleAndroidException;

            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Login);


            Button button = FindViewById<Button>(Resource.Id.Login);
            string accountCode = Resource.Id.AccountCode.ToString();
            string password = Resource.Id.Password.ToString();

            // button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
            button.Click += delegate
            {
                    throw new Exception("LETS THROW A RANDOM EXCEPTION");

            };

    }


    void HandleAndroidException(object sender, RaiseThrowableEventArgs e)
    {
        Log.Error("INTERNAL DEBUG", "PLEASE HANDLE MY EXCEPTION!");
        e.Handled = true;
        System.Console.Write("YOU'VE JUST BEEN HANDLED!");
    }
}

As you can see I am throwing a general exception and attempting to catch this with an UnhandledExceptionRaiser. I used this as a reference: http://androidapi.xamarin.com/index.aspx?link=E%3AAndroid.Runtime.AndroidEnvironment.UnhandledExceptionRaiser

I am able to find my message in the "Android Device Logging" tool, however it is being triggered AFTER an unhandled exception error occurs. I think this means something inside of Xamarin is having first crack at the exception and falling over. Surely there has to be a way of stopping this??

I have seen countless examples online where people have been asking similar questions and there has been no clear solution. Some people have offered some solutions, but these don't actually do what I had anticipated.

It is literally mind boggling to me if this cannot be done.

This is my first time using Xamarin and also my first time developing a mobile app so I apologise if I'm being ignorant about anything.

Please help!!!


回答1:


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;
    }
}


来源:https://stackoverflow.com/questions/27118892/struggling-to-understand-xamarin-exception-handling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!