不多解释,抄书了:
If you don’t catch AggregateException when you call a trigger method, the .NET Framework will escalate the exceptions. By default, this means that the unhandled exceptions will be thrown again when your Task is finalized and cause your program to be terminated.
Because you don’t know when the finalizer will be called, you won’t be able to predict when this will happen. But, you can override the escalation policy and supply your own code to call when an exception is escalated. You do this by adding an event handler
to the static System.Threading.Tasks.TaskScheduler.UnobservedTaskException member.
书里面的实例根本没法用,永远不会触发Task对象的析构!所以我改写了下,而且书里面是2个方法都用了,其实任何一个方法都能处理异常!
using System;
using System.Collections;
using System.Threading.Tasks;
namespace Listing_22
{
class Listing_22
{
static void Main(string[] args)
{
TaskScheduler.UnobservedTaskException +=
(object sender, UnobservedTaskExceptionEventArgs eventArgs) =>
{
// 阻止程序崩溃的方法有2种
//第一种是:
{
eventArgs.SetObserved();
Console.WriteLine("Exception handled");
}
//第二种,返回true
if (false)
{
((AggregateException)eventArgs.Exception).Handle(ex =>
{
Console.WriteLine("Exception handled");
return true;
});
}
};
RunTask();
// 不断分配内存,强制让GC收集Task对象,从而触发UnobservedTaskException
ArrayList arr = new ArrayList();
while (true)
{
char[] array = new char[100000];
arr.Add(array);
GC.Collect();
}
}
private static void RunTask()
{
new Task(() => { throw new NullReferenceException(); }).Start();
}
}
}

当在C#中调用触发方法时不捕获AggregateException,.NET Framework会升级异常。默认情况下,这会在Task析构时重新抛出未处理的异常,导致程序终止。可以通过为System.Threading.Tasks.TaskScheduler.UnobservedTaskException添加事件处理程序来覆盖升级策略,以便在异常被提升时调用自定义代码。书中的示例无法工作,因为Task对象的析构函数永远不会被触发。经过修改,发现实际上只需要一个方法就能处理异常。
1万+

被折叠的 条评论
为什么被折叠?



