【发布时间】:2013-05-14 17:05:03
【问题描述】:
我正在尝试运行以下代码:
class Program
{
static void Main(string[] args)
{
var task = Task.Factory.StartNew(() =>
{
throw new ApplicationException("message");
});
try
{
task.ContinueWith(t => Console.WriteLine("End"));
}
catch (AggregateException aex)
{
Console.Write(aex.InnerException.Message);
}
}
}
我预计Exception 会在以下位置被捕获:
catch (AggregateException aex)
{
Console.Write(aex.InnerException.Message);
}
但这并没有发生。为什么会这样?
【问题讨论】:
-
答案解决了做什么和如何做,而不是“为什么”(未捕获异常)。我认为理解这个基本原理很重要,Stephen Toub 的文章Task Exception Handling in .NET 4.5 对此进行了解释,这是必读]
标签: c# multithreading task-parallel-library