I've been learning to use TPL and have an issue with an example I gathered from this article. I copy and pasted the code exactly as in the Task.Run example but get an error message saying the exception isn't handled:
private async void button1_Click(object sender, EventArgs e)
{
try
{
await Task.Run(() =>
{
Thread.Sleep(1000);
throw new InvalidOperationException("Hi!");
});
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Here's a picture of the error:
Is this code example outdated or am I missing something?
This is just a misleading debugger message.
What's actually happening is that the exception is being thrown, then caught by the .NET framework (not user code), and then placed on the task.
So, while technically the error message is correct (it's caught by the .NET framework - unhandled by user code), it's not very helpful in this case.
Unfortunately, there's also not a lot of alternatives for the debugger. At the point when the exception leaves user code, the debugger has no way of "looking into the future" to know that the exception will be placed on a specific task and that your code will await
that task. It has to make a decision immediately whether to notify you or not, without knowing whether the task will be await
ed.
As mentioned in Stephen's correct answer this happens only when the Debugger is on. Using this simple workaround you can re-throw the exception "outside" of Task.Run();
Exception exceptionOut = null;
await Task.Run(() =>
{
try
{
// Your code
}
catch (Exception exceptionIn)
{
exceptionOut = exceptionIn;
}
});
if (exceptionOut != null)
throw exceptionOut;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With