Take the 2-minute tour ×

I've looked around at a number of the ReactiveUI samples, but I can't see a good simple example of how to handle exceptions, where a message should be displayed to the user. (If there is a good example can somebody point me to it?).

My first question is how to handle an exception with ReactiveCommand and ToProperty. For example, I have the following code:

public class MainWindowViewModel : ReactiveObject
    public ReactiveCommand CalculateTheAnswer { get; set; }

    public MainWindowViewModel()
        CalculateTheAnswer = new ReactiveCommand();

            .SelectMany(_ => AnswerCalculator())
            .ToProperty(this, x => x.TheAnswer);

            .Select(exception => MessageBox.Show(exception.Message));

    private readonly ObservableAsPropertyHelper<int> _theAnswer;
    public int TheAnswer
        get { return _theAnswer.Value; }

    private static IObservable<int> AnswerCalculator()
        var task = Task.Factory.StartNew(() =>
            throw new ApplicationException("Unable to calculate answer, because I don't know what the question is");
            return 42;

        return task.ToObservable();

I think I must be misunderstanding ThrownExceptions, because this observable is not receiving any items when I run the code above. What am I doing wrong?

My second question is how would I do this in a MVVM-friendly way. This blog entry mentions a User Errors feature, but I can't find any documentation on how to use it. How would I implement it into the above example?

Edit: I've published an example solution on github based on Paul's answer below.

share|improve this question
Hey Wayne, this is great! Would you mind contributing this sample to github.com/reactiveui/ReactiveUI.Samples (or give me permission to do so?) –  Paul Betts Dec 25 '12 at 23:00
Hi Paul, you're very welcome to add it to your samples on github. Thanks for the nice framework. –  Wayne Maurer Dec 26 '12 at 21:15

1 Answer 1

up vote 9 down vote accepted

You're understanding ThrownExceptions, but it's on the wrong guy, _theAnswer.ThrownExceptions will receive the Exception. But the tricky part, is now that button doesn't work any more - once an Observable ends OnError, it's done for good.

You end up having to do a few backflips here, something like:

static IObservable<int?> AnswerCalculator()

    .SelectMany(_ => AnswerCalculator())
    .Where(x => x != null)
    .Select(x => x.Value)
    .ToProperty(this, x => x.TheAnswer);

In this case, ReactiveAsyncCommand is much easier, since a new IObservable is created for every invocation, so you'd do:

// ReactiveAsyncCommand handles exceptions thrown for you
CalculateTheAnswer.RegisterAsyncTask(_ => AnswerCalculator())
    .ToProperty(this, x => x.TheAnswer);

CalculateTheAnswer.ThrownExceptions.Subscribe(ex => MessageBox.Show("Aieeeee"));

How to use UserError

So, UserError is like an exception intended to be thrown at a user (i.e. it contains friendly text, not programmer text)

To use UserError, you have to do two things - first, change your ThrownExceptions:

    .SelectMany(ex => UserError.Throw("Something bad happened", ex))
    .Subscribe(result => /* Decide what to do here, either nothing or retry */);

And in your View code-behind, call `RegisterHandler":

UserError.RegisterHandler(err => {

    // This is what the ViewModel should do in response to the user's decision
    return Observable.Return(RecoveryOptionResult.CancelOperation);

The cool part, is that this makes error dialogs testable - in a unit test:

var fixture = new MainWindowViewModel();
bool errorCalled;

using (UserError.OverrideHandlersForTesting(_ => { errorCalled = true; return RecoveryOptionResult.CancelOperation })) { 

share|improve this answer
Thanks for the great answer. It's as good as a blog entry or documentation would be ;-) –  Wayne Maurer Dec 20 '12 at 12:38

Your Answer


