Jonathan
Jonathan

Reputation: 7561

Exception in async task gets intercepted in Visual Studio

I want to run several tasks, some of which would complete async'ly, and then wait for all of them to complete. As the tasks may throw exceptions, I want to catch and log them. sample code for that:

static async Task doit(int x)
{
    try
    {
        Console.WriteLine("{0}  {1}  start", x, Thread.CurrentThread.ManagedThreadId);
        await Task.Run(() =>
        {
            Thread.Sleep(TimeSpan.FromSeconds(2 + x));  // simulate long-running stuff worth awaiting...
            if (x == 3)
            {
                throw new Exception("Exception inside task " + x.ToString());  // Simulate the async task throwing exception
            }
        });

        if (x == 2)
        {
            throw new Exception("Exception after task " + x.ToString());  // Simulate post-async task throwing exception
        }

        Console.WriteLine("{0}  {1}  end", x, Thread.CurrentThread.ManagedThreadId);
    }
    catch (Exception ex)
    {
        Console.WriteLine("{0}  {1}  Exception: {2}", x, Thread.CurrentThread.ManagedThreadId, ex.Message);
    }
}

private static void TestTasks()
{
    var tasks = Enumerable.Range(1, 3).Select(n => doit(n)).ToArray();
    Console.WriteLine("Waiting");
    Task.WaitAll(tasks);
    Console.WriteLine("all end");
}

If I run this code from console, it works as expected, with this output:

1  1  start
2  1  start
3  1  start
Waiting
1  3  end
2  4  Exception: Exception after task 2
3  5  Exception: Exception inside task 3
all end

However, if I debug inside Visual Studio, the debugger stops at the noted exception with Exception was unhandled by user code. Then I need to hit F5 Continue for the code to complete.

I noticed that if I disable Options => Debugger => Enable Just My Code, the debugger doesn't stop. However, I don't want to set this permanently, since some of the frameworks I use handle my exceptions that I do want the debugger to stop on.

Upvotes: 5

Views: 811

Answers (2)

AntoineC
AntoineC

Reputation: 480

Why does the debugger stop even though the exception is inside a try/catch?

This behavior apparently depends on the version of Visual Studio.

I ran into the same issue in Visual 2013. I tried to build and debug the same code with Visual 2015 Update 1. This time, the debugger correctly handled the exception and did not stop.

You did not mention which version of Visual you were using. If it was one older than 2015, you could try using Visual 2015 or newer and see if it fixes the issue.

Upvotes: 0

Stephen Cleary
Stephen Cleary

Reputation: 456777

Why does the debugger stop even though the exception is inside a try/catch?

Technically, the code is throwing an exception that is not caught by any of your code on the current call stack. It's caught by the (compiler-generated) async state machine and placed on the returned task. Later, when the task returned from Task.Run is awaited, that exception is rethrown.

How can I get the debugger not to stop at this exception?

Right now, your only options are to have the debugger ignore this exception in some way (disable just my code, disable breaking on user-unhandled exceptions of that type, ...).

If you think VS should offer a better user experience here, feel free to open a uservoice suggestion.

Upvotes: 5

Related Questions