小编cap*_*hap的帖子

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

目前我正在尝试随.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();
    }
Run Code Online (Sandbox Code Playgroud)

根据文档,Exception应该传播回到调用的连接线程:task1.Wait().

但我总是得到一个未处理的例外:

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

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

c# multithreading exception-handling

2
推荐指数
1
解决办法
2613
查看次数

标签 统计

c# ×1

exception-handling ×1

multithreading ×1