c# - 异常处理(任务并行库).Net Framework 4.0 Beta 2

标签 c# exception multithreading

目前我正在尝试任务并行库的一些新功能, .Net Framework 4.0 Beta 2 随附。

我的问题具体涉及 TPL 中的异常处理 此处描述: http://msdn.microsoft.com/en-us/library/dd997415%28VS.100%29.aspx

第一个例子(稍微改动了一下):

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

        try
        {
            task1.Wait(); // Exception is not handled here....
        }
        catch (AggregateException ae)
        {
            foreach (var e in ae.InnerExceptions)
            {
                Console.WriteLine(e.Message);
            }

        }

        Console.ReadLine();
    }

根据文档,异常应该传播回去 到调用的加入线程:task1.Wait()

但我总是在以下范围内遇到未处理的异常:

var task1 = Task.Factory.StartNew(() =>
{
    throw new MyCustomException("I'm bad, but not too bad!");
});

有人可以向我解释原因吗,或者有人知道自 Beta 2 发布以来是否有什么变化?

最佳答案

答案在您链接的文章中:

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# - 异常处理(任务并行库).Net Framework 4.0 Beta 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2039473/

相关文章:

c# - .NET 框架先决条件

c# - ScrollIntoView - 项目不应从 View 中消失

c# - 如何在类文件中使用BackGroundWorker?

java - 测试线程代码java

c++ - Pthread_create() 不正确的启动例程参数传递

c++ - 线程间共享类的静态数据

c# - 为什么 TextBox 在 WPF 中的菜单上操作时不会失去焦点?

Java异常处理(以json为例)

java - 如何避免使用异常进行流量控制?

exception - 通过插件 Magento2 显示错误信息