Forum Xamarin.Forms

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?

  • @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.

  • No problem, thanks!
  • RezaMohamedRezaMohamed USMember ✭✭✭

    I am in the same boat and would like to have a global error handler to report to appcenter in xamarin.forms for both ios and android. Is there an acceptable solution for this?

  • NMackayNMackay GBInsider, University mod

    Unmanaged exceptions will be caught by AppCenter automatically in most cases, it also detects crashes on restarts.

    For managed exception you should deal with that yourself (at the top UI level) and let the layers below throw exceptions back up through the stack.

    You can have a generic async execution function in your baseviewmodel that handles exceptions and in other exceptions where you need to change UX behavior based on an exception, implement try catch for that area. trying to catch exceptions at the platform specific level if a bit late IMHO and the app can be in too unstable a state to continue in a lot of cases.

  • RezaMohamedRezaMohamed USMember ✭✭✭

    Got it. Is it overkill to use both Rollbar and appcenter crashes for this?

Sign In or Register to comment.