C#-未捕获任务异常 [英] C# - Task Exception not being caught

查看:54
本文介绍了C#-未捕获任务异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定自己做错了什么,但是当我尝试新线程任务异常处理的这个示例时,我总是得到用户代码未处理的异常.该代码的全部重点是显示一个如何捕获任务中的错误的示例.

I am sure I am doinig something wrong, but when I try this example of the new threading task exception handling I keep getting the exception unhandled by user code. The whole point of the code is to show an example of how to catch errors in tasks.

链接:任务异常示例

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

Most likely user error just not sure what (Using Visual Studio 2012);

推荐答案

在您引用的页面中:

注意

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

When "Just My Code" is enabled, Visual Studio in some cases will break on the line that throws the exception and display an error message that says "exception not handled by user code." This error is benign. You can press F5 to continue and see the exception-handling behavior that is demonstrated in these examples. To prevent Visual Studio from breaking on the first error, just uncheck the "Just My Code" checkbox under Tools, Options, Debugging, General.

这篇关于C#-未捕获任务异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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