Catching global exceptions in Android

SiflouSiflou Claude USMember

Hey guys,

I have an Android app where we use flurry to log crash to help us track bugs better. In iOS we Simply put a try/catch block on the UIApplication.Main(args,null,"someDelegate"); and it automatically catches all unhandled exception. I want to do the same with my Android application. I have tried 3 things already that have not worked for me. Here is a code example of what I have used.

`#region TEST_HANDLE_METHODS

    void HandleAndroidException(object sender, RaiseThrowableEventArgs e)
    {
        e.Handled = true;
        Console.Write ("HANDLED EXCEPTION");
    }

    static void HandleExceptions(object sender,UnhandledExceptionEventArgs e)
    {
        //Exception d = (Exception)e.ExceptionObject;
        Console.WriteLine("TEST");
    }

    <a href="/search?Search=%23endregion&Mode=like">#endregion</a>

    <a href="/search?Search=%23region&Mode=like">#region</a> ACTIVITY_LIFE_CYCLE

    protected override void OnCreate (Bundle bundle)
    {
        //
        base.OnCreate (bundle);

        //Test1
        AppDomain currentDomain = AppDomain.CurrentDomain;
        currentDomain.UnhandledException += new UnhandledExceptionEventHandler(HandleExceptions);

        //Test2
        AndroidEnvironment.UnhandledExceptionRaiser +=  HandleAndroidException;

        //Test3
        Java.Lang.Thread.DefaultUncaughtExceptionHandler = new ExceptHandler ();
             }`

The except handle is just a class that derives from Java.Lang.Object and that implements Java.Lang.Tread.IUncaughtExceptionHandler and that has a Console.Writeline("Test"); in the UncaughtException method.
So does anybody has any solutions for this ?

Thanks a lot for your time,

Claude

Posts

  • SiflouSiflou Claude USMember

    Hey there,

    Just found out that The Solution 2 is actually working. What is not working is the Console.WriteLine. I used Android.Util.Log.Debug ("TEST", "Test3"); instead and I got to see my test message. Tried it with solution 1 and 3 but they were still not working so if someone encounters the same problem use my test2 as a solution.

    Thanks a lot and have a great day,

    Claude

  • rmaciasrmacias Ruben Macias USBeta, University ✭✭✭✭✭
    edited February 2014

    When you say "not working", what is it that you are expecting to happen, and what is actually happening?

  • SiflouSiflou Claude USMember

    Well I expected to see the log in the console to see that when I throw an exception that my handler works properly. But as I said, solution 2 actually works it is just the Console.Writeline that is not working, used Android.Util.Log.Debug function and I saw that throwing exception was successfully catched.

    Thanks a lot,

  • rmaciasrmacias Ruben Macias USBeta, University ✭✭✭✭✭

    I don't think you wired your exception handler correctly for Test #1:

    AppDomain currentDomain = AppDomain.CurrentDomain;
    currentDomain.UnhandledException += new UnhandledExceptionEventHandler(HandleExceptions);
    

    Should be:

    AppDomain currentDomain = AppDomain.CurrentDomain;
    currentDomain.UnhandledException += HandleExceptions;
    

    Also, this should be wired up in your Application class. If you're doing this in your Main Activity, your app could crash before the Main Activity has a chance to wire your event handlers, and if your Main Activity gets re-created, you'll be re-wiring the event handlers again (unless you've unwired them when Disposed).

    Also note that by the time these events get fired, the Android OS has already killed your app, or is in the process of killing your app. There's no way to gracefully recover. The only thing you can do is Log your exception details by writing to file or sending it to a web service.

    Take a look at this sample and it'll give you some ideas.

    https://github.com/xamarin/monodroid-samples/tree/master/AdvancedAppLifecycleDemos/HandlingCrashes

  • SiflouSiflou Claude USMember

    Thanks a lot rmacis, I'll check this out :)

  • ThomasFlemmingThomasFlemming Thomas Flemming DEMember

    Thanks for this explaination. Its very useful to write the exception-message to a file in the UnhandledException handler.

    Is there also a way to inform the user about this? Showing an AlertDialog or Nessagebox?
    Since the app.context is gone already, I can't use any Android-functions anymore.
    Tom

  • AjaySharma.5121AjaySharma.5121 Ajay Sharma USMember

    @RubenMacias : If our application have killed before going to Global Exception Handler then how can I prevent my application to become crash ?

  • rmaciasrmacias Ruben Macias USBeta, University ✭✭✭✭✭

    @AjaySharma.5121 you don't unfortunately. Android doesn't work that way.

  • AjaySharma.5121AjaySharma.5121 Ajay Sharma USMember
    edited January 2016

    @RubenMacias : Thanks for reply. It means to prevent my application become crash, I need to use try catch block on my each UI code where there may be a chance to get crash ?

  • rmaciasrmacias Ruben Macias USBeta, University ✭✭✭✭✭
    edited January 2016

    You could do that, but that doesn't guarantee protection from exceptions raised from the Dalvik/ART side. There's no 100% fail safe to protect an Android app from crashing.

Sign In or Register to comment.