这是我需要帮助的面试问题.
您有以下ASP.NET代码隐藏类:
public partial class Page1 : Page { private string _value; public Page1() { if (DateTime.Now.Ticks % 10 == 0) _value = "Test"; } ~Page1() { if(_value.Equals("Test")) _value = string.Empty; } }
任何有人请求此页面,w3wp.exe进程意外终止.
>为什么会发生与用户看到黄色屏幕死亡(默认的ASP.NET错误页面)?@H_404_9@>为什么托管堆上总是出现OutOfMemoryException?
解决方法
提示:永远不会在析构函数/ finalizer中抛出异常,否则将会杀死GC运行的线程,而GC中的线程可能会变得丑陋.
虽然在.NET 1.1中有一些容忍在后台线程中抛出的异常,这些异常已被使用,并且不会使主机进程停止,但从CLR 2.0开始就不再这样了. Quote from the doc:
If Finalize or an override of Finalize@H_404_9@ throws an exception,and the runtime@H_404_9@ is not hosted by an application that@H_404_9@ overrides the default policy,the@H_404_9@ runtime terminates the process and no@H_404_9@ active try-finally blocks or@H_404_9@ finalizers are executed. This behavior@H_404_9@ ensures process integrity if the@H_404_9@ finalizer cannot free or destroy@H_404_9@ resources.
在终结者中抛出异常是致命的.