【问题标题】:C# - Task Exception not being caughtC# - 未捕获任务异常
【发布时间】:2012-10-23 09:04:08
【问题描述】:

我确定我做错了什么,但是当我尝试这个新线程任务异常处理的示例时,我不断收到用户代码未处理的异常。代码的重点是展示如何捕获任务中的错误的示例。

链接:Task Exception Example

static void Main(string[] args)
        {
            var task1 = Task.Factory.StartNew(() =>
            {
                throw new MyCustomException("I'm bad, but not too bad!");
            });

            try
            {
                task1.Wait();
            }
            catch (AggregateException ae)
            {
                // Assume we know what's going on with this particular exception. 
                // Rethrow anything else. AggregateException.Handle provides 
                // another way to express this. See later example. 
                foreach (var e in ae.InnerExceptions)
                {
                    if (e is MyCustomException)
                    {
                        Console.WriteLine(e.Message);
                    }
                    else
                    {
                        throw;
                    }
                }

            }
        }

最有可能的用户错误只是不确定是什么(使用 Visual Studio 2012);

【问题讨论】:

  • 滚动到黄色大框中的“注意”。 :)

标签: c#


【解决方案1】:

来自您引用的页面:

注意

启用“仅我的代码”后,Visual Studio 在某些情况下会 在引发异常的行上中断并显示错误 显示“用户代码未处理异常”的消息。这个错误是 良性。您可以按 F5 继续并查看异常处理 这些示例中展示的行为。为了防止视觉 Studio 从第一个错误中中断,只需取消选中“Just My 工具、选项、调试、常规下的“代码”复选框。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 2017-03-23
    相关资源
    最近更新 更多