0

I have an async method that is throwing. This has no impact on my app, I see the exception in the debugger output window.

I can turn on first chance exceptions and have the VS debugger catch it in the expected place, i.e. when the call returns at the await keyword.

However, I'd expect this exception to crash my app.

Starting with the .NET Framework version 2.0, the common language runtime allows most unhandled exceptions in threads to proceed naturally. In most cases this means that the unhandled exception causes the application to terminate.

Why doesn't it? -- I have found the answer so I'll post Q/A style to keep it useful for others.

Luke Puplett
  • 42,091
  • 47
  • 181
  • 266

1 Answers1

0

The cause is that I am several calls deep from an async void. The exception is being caught and placed into the Task that's returned from the current asynchronous method and that process occurs for each asynchronous call up the stack.

The problem comes when it reaches async void, there's no Task object through which exceptions can be marshalled back to the main process thread. Hence it is swallowed.

Given that due to the event-driven nature of UI work, its common to have an async void at the top of the stack and so this is a gotcha that's going to be hit a fair few times.

From now on, I'll ensure I wrap a try catch around code inside event handlers.

http://tomasp.net/blog/csharp-async-gotchas.aspx/

Luke Puplett
  • 42,091
  • 47
  • 181
  • 266