cap*_*hap 2 c# multithreading exception-handling
目前我正在尝试随.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发布以来是否有某些变化?
答案在你链接的文章中:
启用"Just My Code"时,Visual Studio在某些情况下会在抛出异常的行上中断,并显示一条错误消息,指出"异常不由用户代码处理".这个错误是良性的.您可以按F5继续并查看这些示例中演示的异常处理行为.要防止Visual Studio在第一个错误中出现问题,只需取消选中Tools,Options,Debugging,General下的"Just My Code"复选框.
| 归档时间: |
|
| 查看次数: |
2613 次 |
| 最近记录: |