异步任务中的异常在Visual Studio中被拦截 [英] Exception in async task gets intercepted in Visual Studio

查看:142
本文介绍了异步任务中的异常在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

但是,如果我在Visual Studio中进行调试,则调试器将在指定的异常处停止,原因是用户代码未处理异常。然后我需要点击 F5继续来完成代码。

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.

我注意到,如果我禁用Options =>调试器=> 仅启用我的代码,调试器不会停止。但是,我不想永久设置此设置,因为我使用的某些框架可以处理我想让调试器停止运行的异常。

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.


  • 即使异常在try / catch内,调试器为什么也会停止?

  • 如何使调试器在此异常下不停止?

推荐答案


即使异常发生,调试器为什么也会停止是在try / catch里面吗?

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

从技术上讲,代码抛出的异常没有被 your 当前调用堆栈上的代码。它(由编译器生成) async 状态机捕获,并放置在返回的任务上。后来,当 Task.Run 返回的任务被 await ed时,该异常是 re throw。

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, ...).

如果您认为VS应该在这里提供更好的用户体验,请随时打开用户语音建议。

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

这篇关于异步任务中的异常在Visual Studio中被拦截的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆