Global Exception Handling

Hi Everyone. I'm in my final submission of my Xamarin forms App. I want to handle Exceptions Globally in my Project. I tried this https://peterno.wordpress.com/2015/04/15/unhandled-exception-handling-in-ios-and-android-with-xamarin/ but it's not working.

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

The above methods are not invoking when any exception raised. Any kind of help will be appreciated.

Answers

  • JohnHardmanJohnHardman GBUniversity mod

    @MohammedDastagir said:
    I'm in my final submission of my Xamarin forms App. I want to handle Exceptions Globally in my Project.

    Exceptions are best handled close to the point where they are raised.
    Global handlers exist to allow you to be notified of exceptions that you have not handled elsewhere. They do not exist to allow you to catch an exception and continue execution as if nothing had happened.
    If you do not handle exceptions near to where they are raised and one of the global handlers is fired, do not expect to be able to recover. Your app will likely be in an unknown state at that point, so it is likely to be unsafe to continue execution.

    Exception handling is best added early in development. Trying to retro-fit it at the end of development is going to be painful.

    What does "it's not working" mean? What are you doing that you expect to invoke one of the handlers? What (if anything) is happening?

    Note that TaskScheduler.UnobservedTaskException is not fired immediately. You may have to wait a while before it fires.

    Note that as well as the two handlers that you have mentioned, there are also platform-specific ones.

    Android
    AndroidEnvironment.UnhandledExceptionRaiser += AndroidEnvironmentOnUnhandledException;

    UWP
    Windows.UI.Xaml.Application.Current.UnhandledException += OnUnhandledException;

  • jezhjezh Member, Xamarin Team Xamurai

    Could you please post a basic demo that you have added the Global Exception Handling so that we can test it?

  • ciarangallagher036ciarangallagher036 Member ✭✭

    @JohnHardman I understand it is better to catch exceptions near the source of the error, but I would still like to be able to handle exceptions which were not caught elsewhere.

    In that case, in my MainActivity.cs I registered my events and implemented the events to log my exceptions to the Xamarin App Center. On a logon screen, I added code to simply 'throw new Exception("Whatever")'. My expectation then would be that the Android unhandled exception raiser event would be fired, but it doesn't seem to get fired?

    What else could I be missing?

    Thanks.

  • JohnHardmanJohnHardman GBUniversity mod

    @ciarangallagher036 said:
    What else could I be missing?

    You're not missing anything. A change was made some time back that prevents the Android unhandled exception handler from being called. There's long discussion about this on another forum thread.

    I'm just in the midst of updating a complex project from PCL to .Net Standard 2. I'm re-factoring some stuff in the process. I'll re-test the unhandled exception handler once I'm done, but I'm not expecting it to suddenly start working again unfortunately.

  • ciarangallagher036ciarangallagher036 Member ✭✭
    No problem, thanks!
Sign In or Register to comment.