Miscellaneous Debris

Avner Kashtan's Frustrations and Exultations

Static Constructor throws the same exception again and again

Here’s a little gotcha I ran into today – if you have code in a class’s static constructor that throws an exception, we will get a TypeInitializationException with the original exception as the InnerException – so far, nothing new.

However, if we keep on calling methods on that object, we’ll keep receiving TypeInitializationExceptions. If it cannot be initialized, it cannot be called. Every time we try, we’ll receive the exact same exception. However, the static ctor will not be called again. What appears to happen is that the CLR caches the TypeInitializationException object itself, InnerException included, and rethrows it whenever the type is called.

What are the ramifications? Well, we received an OutOfMemoryException in our static ctor, but the outer exception was caught and tried again. So we got an OutOfMemoryException again, even though the memory problem was behind us, which sent us down the wrong track of looking for persistent memory problems. Theoretically, it could also be a leak – the inner exception holding some sort of reference that is never released, but that’s an edge case.

Here’s some code to illustrate the problem. The output clearly shows that while we wait a second between calls, the inner exception contains the time of the original exception, not any subsequent calls. Debugging also shows that the static ctor is called only once.

   1:  class Program
   2:  {
   3:      static void Main(string[] args)
   4:      {
   5:   
   6:          for (int i = 0; i < 5; i++)
   7:          {
   8:              try
   9:              {
  10:                  Thread.Sleep(1000);
  11:                  StaticClass.Method();
  12:   
  13:              }
  14:              catch (Exception ex)
  15:              {
  16:                  Console.WriteLine(ex.InnerException.Message);
  17:              }
  18:          }
  19:   
  20:          Console.ReadLine();
  21:      }
  22:  }
  23:   
  24:  static class StaticClass
  25:  {
  26:      static StaticClass()
  27:      {
  28:          throw new Exception("Exception thrown at " + DateTime.Now.ToString());
  29:      }
  30:   
  31:      public static void Method()
  32:      { }
  33:   
  34:  }

Comments

AndrewSeven said:

I can't find the official doc, but I've always believed that a static initialization exception rendered they type unusable for the life of the app domain.

# December 16, 2008 9:05 AM

Avner Kashtan said:

It does, if by "unusable" you mean "throws a TypeInitializationException for the life of the app domain" and by "throws a TypeInitializationException" you mean "throws THE SAME TypeInitializationException instance". :)

# December 16, 2008 9:59 AM

Fique por dentro Constructor » Blog Archive » Static Constructor throws the same exception again and again … said:

Pingback from  Fique por dentro Constructor  &raquo; Blog Archive   &raquo; Static Constructor throws the same exception again and again &#8230;

# January 12, 2009 8:30 PM

Fique por dentro Constructor » Blog Archive » Static Constructor throws the same exception again and again … said:

Pingback from  Fique por dentro Constructor  &raquo; Blog Archive   &raquo; Static Constructor throws the same exception again and again &#8230;

# January 17, 2009 4:35 AM

Visual Guard for Sybase ASE said:

Even I think they are unusable for the rest of the life of app domain.

# March 10, 2009 5:23 AM

RoseGunish said:

Wonderful article as for me. Keep posting such stuff!

Rose Gunish

<a href="bratislava-escort.com/">escort a bratislava</a>

# October 1, 2011 2:36 PM

MarryFrakloop said:

Wonderful stuff  at least I thing so. Keep writing such stuff!

Marry Frakloop

<a href="bratislava-escort.com/">escort service bratislava</a>

# October 1, 2011 6:08 PM

Anna23Wilson said:

Nice post  at least I thing so. Keep it up!

Anna 23Wilson

<a href="cyprusescorts.us/">exclusive cyprus escorts agency</a>

# October 29, 2011 5:57 PM

RoseBodge said:

Great post  at least I thing so. Keep posting such stuff!

Rose Bodge

<a href="delhiescortservice.com/">girls escorts in delhi</a>

# October 29, 2011 9:40 PM

RoseBodge said:

Cool blog to my mind. Keep posting such stuff!

Rose Bodge

<a href="escort-brazil.com/">escort services in brazil</a>

# November 2, 2011 2:40 PM

SussyFrakloop said:

Great post to my mind. Keep it up!

Sussy Frakloop

<a href="cyprusescorts.us/">cyprus escort services independent escorts</a>

# November 5, 2011 3:07 PM

Mary said:

Yeah, it happens sometimes ... Nothing special.

# March 27, 2012 3:37 PM

Amanda said:

Yeah, it happens sometimes ... Nothing special.

# March 27, 2012 3:37 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)