我知道使用 Task
时如何处理未处理的异常s,如果用户代码还没有“观察到”它,只会在终结器中抛出一个未处理的。
我也知道异步线程中未处理的异常(例如 Action.BeginInvoke()
)是如何在加入调用(例如 Action.EndInvoke()
)中被捕获并重新抛出的。
我不明白的是这如何不会使过程崩溃?
static void Main(string[] args)
{
var timer = new System.Timers.Timer() {Interval = 100};
timer.Elapsed += (o, e) => { throw new Exception(); };
timer.Start();
Console.ReadKey( true );
}
最佳答案
来自 .NET 4.0 文档:
In the .NET Framework version 2.0 and earlier, the Timer component catches and suppresses all exceptions thrown by event handlers for the Elapsed event. This behavior is subject to change in future releases of the .NET Framework.
http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
目前还没有声明声称这种行为实际上已经改变。
关于.net - 为什么计时器线程上未处理的异常不会使进程崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9407062/