private static void List_19_Basic_Exception_Handling()
{
Task task1 = new Task(() =>
{
for (int i = 0; i < 5; i++)
{
ArgumentOutOfRangeException exception = new ArgumentOutOfRangeException();
exception.Source = "task1";
throw exception;
}
});
Task task2 = new Task(() =>
{
throw new NullReferenceException();
});
Task task3 = new Task(() =>
{
Console.WriteLine("Hello from Task 3");
});
task1.Start();
task2.Start();
task3.Start();
try
{
Task.WaitAll(task1, task2, task3);
}
**catch (AggregateException ex) // 这里始终无法捕获异常,不知道为什么**
{
foreach (Exception inner in ex.InnerExceptions)
{
Console.WriteLine("Exception type {0} from {1}", inner.GetType(), inner.Source);
}
}
}
static void Main(string[] args)
{
List_19_Basic_Exception_Handling();
// wait for input before exiting
Console.WriteLine("Main method complete. Press enter to finish.");
Console.ReadLine();
}
我是在VS2010中运行时出现这种问题,不知道为什么无法捕获导演,有知道原因大牛,欢迎留言,谢谢!:)