Skip to content

Default Exception Handler

The default behavior of ReactiveUI is to crash the application with whenever an object that has a ThrownExceptions property doesn't have a subscription.

You can override this behavior or hook your debugger or analytics client by registering a custom IObserver<Exception> through RxAppBuilder — it wires the handler into the read-only RxState.DefaultExceptionHandler slot:

RxAppBuilder.CreateReactiveUIBuilder()
    .WithExceptionHandler(new MyCoolObservableExceptionHandler())
    .BuildApp();

The handler itself is an IObserver<Exception>:

public class MyCoolObservableExceptionHandler : IObserver<Exception>
{
    public void OnNext(Exception value)
    {
        if (Debugger.IsAttached) Debugger.Break();

        Analytics.Current.TrackEvent("MyRxHandler", new Dictionary<string, string>()
                                                {
                                                    {"Type", value.GetType().ToString()},
                                                    {"Message", value.Message},
                                                });

        RxSchedulers.MainThreadScheduler.Schedule(() => { throw value; }) ;
    }

    public void OnError(Exception error)
    {
        if (Debugger.IsAttached) Debugger.Break();

        Analytics.Current.TrackEvent("MyRxHandler Error", new Dictionary<string, string>()
        {
            {"Type", error.GetType().ToString()},
            {"Message", error.Message},
        });

        RxSchedulers.MainThreadScheduler.Schedule(() => { throw error; });
    }

    public void OnCompleted()
    {
        if (Debugger.IsAttached) Debugger.Break();
        RxSchedulers.MainThreadScheduler.Schedule(() => { throw new NotImplementedException(); });
    }
}