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 = new List<Task>();
for (int i = 1; i <= 3; i++)
{
tasks.Add(doit(i));
}
Console.WriteLine("Waiting");
Task.WaitAll(tasks.ToArray());
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.
答案 0 :(得分:2)
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 await
ed, 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.
答案 1 :(得分:0)
为什么调试器会停止,即使异常位于try / catch中?
此行为显然取决于Visual Studio的版本。
我在Visual 2013中遇到了同样的问题。我尝试使用Visual 2015 Update 1构建和调试相同的代码。这次,调试器正确处理了异常并且没有停止。
您没有提到您使用的是哪个版本的Visual。如果它比2015年更早,您可以尝试使用Visual 2015或更新版本,看看它是否解决了问题。