Handle ThrownExceptions when using ExecuteAsync?

118 views
Skip to first unread message

Mark Hewett

unread,
Mar 5, 2015, 1:48:12 PM3/5/15
to reacti...@googlegroups.com
Hopefully you can all handle the VB.NET without feeling too dirty! :-)

I have a WPF application, .NET 4.5.2, ReactiveUI v6.4.0.1 from NuGet.  I am trying to handle exceptions thrown by my commands.  This works OK when my command is triggered by a button click, but I also want to execute the command when my view is loaded.  Here's the basic setup:

ViewModel constructor:

Me.FetchData = ReactiveCommand.CreateAsyncTask(
Async Function(x)
Return Await FunctionThatThrowsAnException()
End Function
)
Me.FetchData.ToProperty(Me, Function(x) x.MyData, _myData)
' Just an example of error handling, will use UserError and handle in view...
Me.FetchData.ThrownExceptions.Subscribe(
Sub(ex) Me.Log().ErrorException(ex.Message, ex)
)
' I've also tried this here...
'Me.FetchData.ExecuteAsync().Subscribe()

View constructor:

Me.WhenAnyValue(
Function(x) x.ViewModel.FetchData
).SelectMany(
Function(x) x.ExecuteAsync()
).Subscribe()

The result is that the exception is thrown, the log message (from the ThrownExceptions handler) is printed, and the application terminates.  If I remove the ExecuteAsync call from the view constructor, and just click the button to fetch the data (bound to the same command), then the message is printed and the application stays running.

So, what am I missing about handling ThrownExceptions when calling ExecuteAsync?  Please point out anything else the indicates I haven't a clue, since I am really struggling to grok ReactiveUI at this point!

Thanks!
Mark

paul....@gmail.com

unread,
Mar 9, 2015, 1:47:17 AM3/9/15
to reacti...@googlegroups.com, reacti...@googlegroups.com
Hey Mark,

This is an annoying design aspect of ExecuteAsync, that it ends up reporting the exception to the caller *and* to ThrownExceptions. I’ll probably fix this at some point.

Best,
Paul



--
You received this message because you are subscribed to the Google Groups "ReactiveUI mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to reactivexaml...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mark Hewett

unread,
Mar 9, 2015, 11:22:07 AM3/9/15
to reacti...@googlegroups.com
On Monday, March 9, 2015 at 12:47:17 AM UTC-5, Paul Betts wrote:
Hey Mark,

This is an annoying design aspect of ExecuteAsync, that it ends up reporting the exception to the caller *and* to ThrownExceptions. I’ll probably fix this at some point.

Best,
Paul

Thanks Paul.  So, is the correct way to handle that to add a Catch to my WhenAnyValue in the view?  i.e.


Me.WhenAnyValue(
Function(x) x.ViewModel.FetchData
).SelectMany(
Function(x) x.ExecuteAsync()
).Catch(
Observable.Return(Of MyDataType)(Nothing)
).Subscribe()

Also, when using WhenAnyValue like that in my view, to observe viewmodel properties, do I need to worry about disposing
the value returned from Subscribe to avoid leaking anything? It's that sort of thing that is confusing me the most at
the moment! I tried putting the above inside WhenActivated, but that seems to get called both when navigating to the
view and when navigating away from it.

Thanks again,
Mark
Reply all
Reply to author
Forward
0 new messages