Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Try/Catch Wrap Around Task.Run not Handling Exception

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:

enter image description here

Is this code example outdated or am I missing something?

like image 383
Seapoe Avatar asked May 17 '16 17:05

Seapoe


2 Answers

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 awaited.

like image 75
Stephen Cleary Avatar answered Oct 06 '22 09:10

Stephen Cleary


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;
like image 1
Programmierus Avatar answered Oct 06 '22 09:10

Programmierus