Handle Crash Gracefully

NipunKalraNipunKalra USMember ✭✭

Hello Everyone,
I am trying to implement unhandled exception in Xamarin Form.
Something similar to this https://github.com/Ereza/CustomActivityOnCrash.
So when the app is about to crash display a nice message to inform the user that something went wrong.
Option to restart or send the crash report.

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited May 13

    You can add the following code in Android&IOS to catch unhandle exception.

    AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
    TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
    

    Tutorial: https://docs.microsoft.com/en-us/dotnet/api/system.appdomain.unhandledexception?view=netframework-4.8
    Similar issue: https://stackoverflow.com/a/39504448

  • JohnHardmanJohnHardman GBUniversity mod

    @yelinzh said:
    You can try to add the following code in Android&IOS to catch unhandle exception.

    AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
    TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
    

    Tutorial: https://docs.microsoft.com/en-us/dotnet/api/system.appdomain.unhandledexception?view=netframework-4.8
    Similar issue: https://stackoverflow.com/a/39504448

    As per other threads on this subject, Whilst AppDomain.CurrentDomain.UnhandledException used to get fired as expected, it does not get fired any more (I'm using XF 3.4) on Android & iOS (it does on UWP).

    cc @batmaci @NipunKalra

  • NipunKalraNipunKalra USMember ✭✭

    @yelinzh said:

    You can try to add the following code in Android&IOS to catch unhandle exception.

    AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
    TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
    

    Tutorial: https://docs.microsoft.com/en-us/dotnet/api/system.appdomain.unhandledexception?view=netframework-4.8
    Similar issue: https://stackoverflow.com/a/39504448

    Thank you for your reply but this doesn't suffice what is in the link. I have handled these 2 events to generate the logs. But if you follow the link, it shows a nice display to inform the user that something went wrong

  • NipunKalraNipunKalra USMember ✭✭

    @JohnHardman said:

    @yelinzh said:
    You can try to add the following code in Android&IOS to catch unhandle exception.

    AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
    TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
    

    Tutorial: https://docs.microsoft.com/en-us/dotnet/api/system.appdomain.unhandledexception?view=netframework-4.8
    Similar issue: https://stackoverflow.com/a/39504448

    As per other threads on this subject, Whilst AppDomain.CurrentDomain.UnhandledException used to get fired as expected, it does not get fired any more (I'm using XF 3.4) on Android & iOS (it does on UWP).

    cc @batmaci @NipunKalra

    I am using XF 3.5.0.169047 and the events get triggered and my methods are getting invoked

  • batmacibatmaci DEMember ✭✭✭✭✭

    @NipunKalra you should read the other thread till you understand the point. nobody claimed that they arent fired but they dont prevent the crash. they are useless

  • JohnHardmanJohnHardman GBUniversity mod

    @batmaci said:
    @NipunKalra you should read the other thread till you understand the point. nobody claimed that they arent fired but they dont prevent the crash. they are useless

    Apologies, I probably confused things in my post above - @batmaci is correct in that the app terminates even when the handlers are used on Android & iOS, rather than the handler not getting called. #longday

  • batmacibatmaci DEMember ✭✭✭✭✭
    edited May 8

    there is even disadvantage using those handlers. if you dont use, you get more information in the appcenter for a crash. so it crashes in either cases but if you dont use it, appcenter logs it better.

  • NipunKalraNipunKalra USMember ✭✭

    @batmaci I haven't said I want to prevent the crash, But before the app gets close I want to log the stack trace and don't want to display the default platform message which says the app has stopped but to show a nice screen and in background log the errors which use above-mentioned events for me.

    I am not using AppCenter, the decision of not using is not in my hands for now.
    Another example I found for android. This is the behaviour I was looking to implement in Xamarin form. Just want some direction
    https://android.jlelse.eu/handling-uncaught-exceptions-in-android-d818ffb20181

  • JarvanJarvan Member, Xamarin Team Xamurai

    How about trying the errlog.io plugin?

    Tutorial: https://www.errlog.io/docs/xamarin-forms

  • NipunKalraNipunKalra USMember ✭✭

    @yelinzh Thank you for the suggestion. I would prefer AppCenter over errlog.

  • batmacibatmaci DEMember ✭✭✭✭✭
    edited May 14

    @NipunKalra thats what we are talking about you cant prevent default "your app is crashed" message from android. on Ios i dont know but on android at least i have tested different scenarios and it doesnt work. i doubt that you can display even a message or popup. if you achieve this, please share with us how you do it. i would love to see it.

    beside that your logging will have less information than without using those handlers. if you use app.center, it reports as a crash and stacktrace is richer

Sign In or Register to comment.