#817 – What Happens When a Static Constructor Throws an Exception

If any exception is thrown from within a static constructor, a TypeInitializationException will be thrown, with an InnerException set to the original exception that occurred.  At that point, you can no longer access any static data or methods in the class.  You also can’t create any objects of the type using instance constructors.  After the exception, the type is unusable.

    public class Dog
    {
        public static string DogMotto { get; set; }

        static Dog()
        {
            DogMotto = "We serve humans";
            throw new Exception("Oh no");
        }
    }
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine(Dog.DogMotto);
                Console.WriteLine("After DogMotto output");
            }
            catch (Exception xx)
            {
                Console.WriteLine("Error: {0}", xx.ToString());
            }

817-001

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

3 Responses to #817 – What Happens When a Static Constructor Throws an Exception

  1. Pingback: Dew Drop – April 8, 2013 (#1,522) | Alvin Ashcraft's Morning Dew

  2. NArjis says:

    Hi, Can you also demonstrate what happens if exception is caught in the static constructor and what is the the state of object after an instance is created? thanks in advance

    • Sean says:

      If you catch an exception in your static constructor, what happens completely depends on what you were doing in the constructor. Whatever initialization was done prior to the exception is still valid, but obviously any static initialization that you would have done after the point where the exception was thrown won’t get done. Static members of the class will be usable at that point, but with the caveat that you may not have done all of the initialization that you expect to do.

      In general, you wouldn’t want to catch exceptions in the constructor unless you could guarantee to client code that the class’ static members are ready to use.

      “What is the state of [the] object after an instance is created”? That depends, again, on what initialization you managed to do before your exception was thrown and whether the initialization that you were doing has any relation to instances of the class. I.e. Does an instance rely on static data that you would have initialized after the point where the exception was thrown?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: