Web developers wiki ASP.NET Sitecore Sharepoint Kentico by Evident Interactive

Unhandled exception in a destructor crashes IIS process

Modified: 2009/07/10 11:18 by vanthoog - Categorized as: ASP.NET
Recently at a seminar someone casually said something like “in ASP.NET if an exception is not handled in a destructor of a class, the IIS process will crash”. I couldn’t really believe this, so I decided to try it myself. And guess what, he is right.

To see this for yourself try the following….

Within a ASP.NET application create a aspx page, e.g. CrashDemo.aspx, and add the following line of code:

<asp:Label ID="DummyLabel" runat="server" />

This label isn’t really important. We will only use it to display something when IIS is not crashing.

Next, in the codebehind or in a separate class-file, add the following class:

    public class CrashClass
    {
        public string Name { get; set; }

        public bool ThrowExceptionInDestructor { get; set; }
        public bool HandleExceptionInDestructor { get; set; }

        public CrashClass(string name)
        {
            this.Name = name;
        }

        ~CrashClass()
        {
            this.Name = null;

            if (this.ThrowExceptionInDestructor)
            {
                if (!HandleExceptionInDestructor)
                {
                    throw new ApplicationException("unhandled exception in CrashClass destructor");
                }
                else
                {
                    try
                    {
                        throw new ApplicationException("handled exception in CrashClass destructor");
                    }
                    catch
                    {
                        // just ignore
                    }
                }
            }
        }
    }


And finally add the following code snippet to the Page_Load of your aspx page:

        protected void Page_Load(object sender, EventArgs e)
        {
            for (int ii = 0; ii < 1000; ii++)
            {
                CrashClass tempObj = new CrashClass(ii.ToString());
                tempObj.ThrowExceptionInDestructor = true;
                tempObj.HandleExceptionInDestructor = false;

                DummyLabel.Text += tempObj.Name + " ";
            }
        }


Now compile your code and call your aspx page several times in a browser. You will see that the IIS process will fail. On Windows XP this will be clearly visible in the application log, in which the following error will be reported:

Error in eventlog when IIS process has crashed.

Error in eventlog when IIS process has crashed.


You may have to call this aspx page several times before IIS crashes. This is because the destructor of the class CrashClass will only be activated when the garbage collector decides to clean up an object based on this class. And you will never know when this is going to happen. To force the garbage collector to clean up objects more rapidly a loop is used in the Page_Load to create many objects.

To following two lines of code in the Page_Load allow you to play a little bit with this behavior:

                tempObj.ThrowExceptionInDestructor = true;
                tempObj.HandleExceptionInDestructor = false;

In this code snippet if you set the property ThrowExceptionInDestructor to false, the destructor of CrashClass will not throw an exception and hence the IIS process will not crash. Likewise if you set the properties ThrowExceptionInDestructor and HandleExceptionInDestructor both to true, the destructor of CrashClass will still throw an exception but the exception will be handled in the destructor causing the IIS process not to crash.

So in conclusion, within ASP.NET if an unhandled exception is thrown in the destructor of a class, the IIS process will crash. However, if an exception is handled (i.e. caught) properly within the destructor, there will be no problem and the IIS process will continue to run without any problems.

 © Evident Interactive BV