async void verses asnyc Task

Topics: Actions & Coroutines
Dec 19, 2013 at 4:16 PM
I am having issues with exceptions being swallowed. I have a single member which is bound to via Caliburn.Micro conventions. If this method is an 'async void' then the exception triggers the Application_UnhandledException event handler and I can deal with it. If this member is an 'async Task' then the exception is swallowed. The application does not crash, it just continues on as if nothing happened.

I posted on StackOverflow and Stephen Cleary's answer helped me understand what it going on. If the member does not return a task, then it is "re-raised on the SynchronizationContext that represents the request context, and that's why they're passed to Application.UnhandledException."

If the member does return a task, then any exceptions that occur within it are packaged into the Task where they can be handled by the caller. The caller in this case would be Caliburn.Micro.

I did see a reference here to the Coroutine.Completed event but am unsure as to how I would use this to solve my problem.

What is the guidance here? Should I always be using async voids if I am going to bind to a member with Caliburn.Micro? Is there a way of handling the exceptions that are thrown if async Tasks are used?