Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struggling to understand Xamarin exception handling

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!!!

like image 493
Sami.C Avatar asked Nov 25 '14 04:11

Sami.C


People also ask

What are the advantages of exception handling in C#?

Exception Handling in C# is a process to handle runtime errors. We perform exception handling so that normal flow of the application can be maintained even after runtime errors. In C#, exception is an event or object which is thrown at runtime. All exceptions the derived from System.

What is exception handling in asp net?

In the . NET Framework, an exception is an object that inherits from the System. Exception class. An exception is thrown from an area of code where a problem has occurred. The exception is passed up the call stack to a place where the application provides code to handle the exception.


1 Answers

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;
    }
}
like image 94
Alex.F Avatar answered Sep 21 '22 12:09

Alex.F