问题
I know how to handle exceptions thrown by async tasks called by ReactiveCommand<T>
but how do I handle an exception that is thrown before the task is returned?
In the following example ThrowAndHandle
command will throw an exception from the async task when executed and the exception will be handled. The command ThrowButFailToHandle
demonstrates that I can not use ThrownExceptions
to handle an exception that does not occurr "in" the task but rather before the task is created. How can such an exception be handled?
public class ViewModel
{
public IReactiveCommand ThrowAndHandle { get; private set; }
public IReactiveCommand ThrowButFailToHandle { get; private set; }
public ViewModel()
{
ThrowAndHandle = ReactiveCommand.CreateAsyncTask(_ => ThrowFromTask());
ThrowAndHandle.ThrownExceptions.Subscribe(HandleException);
ThrowButFailToHandle = ReactiveCommand.CreateAsyncTask(_ => ThrowBeforeTaskIsReturned());
ThrowButFailToHandle.ThrownExceptions.Subscribe(ThisMethodWillNotBeCalled);
}
private Task ThrowFromTask()
{
return Task.Run(() =>
{
throw new Exception("This exception will appear in IReactiveCommand.ThrownExceptions");
});
}
private Task ThrowBeforeTaskIsReturned()
{
throw new Exception("How can I handle this exception?");
}
private void HandleException(Exception ex)
{
// This method is called when ThrownFromTask() is called
}
private void ThisMethodWillNotBeCalled(Exception ex)
{
}
}
回答1:
Assuming your commands are directly bound to UI, the short answer is you can't.
The exception will be propagated to the onError handler of ExecuteAsync
observable, which is ignored as per the implementation of Execute
:
public void Execute(object parameter)
{
ExecuteAsync(parameter).Catch(Observable.Empty<T>()).Subscribe();
}
Now if you deeply need to catch this exception, you can certainly:
- wrap the ReactiveCommand into an ICommand, with a different
Execute
behavior upon error - wrap the lambda passed to
CreateAsyncCommand
to return a failure task result upon exception - issue/PR on reactiveui to propagate these exceptions also to
ThrownExceptions
来源:https://stackoverflow.com/questions/32987488/how-to-catch-exception-from-reactivecommand