2

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

链接:任务异常示例

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);

4

1 回答 1

15

从您引用的页面:

笔记

启用“仅我的代码”后,Visual Studio 在某些情况下会在引发异常的行中断并显示一条错误消息,指出“用户代码未处理异常”。这个错误是良性的。您可以按 F5 继续并查看这些示例中演示的异常处理行为。为防止 Visual Studio 因第一个错误而中断,只需取消选中Tools、Options、Debugging、General下的“Just My Code”复选框。

于 2012-10-23T01:04:08.233 回答