Monday, July 15, 2013

#878 – Unhandled Exceptions in Static Constructors

Inline image 1

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.  If this exception is not caught, the application will be terminated.

public class Dog
{
    public static string Motto { get; set; }
 
    static Dog()
    {
        Motto = "Serve humans and chase balls";
        throw new Exception("Static Dog constructor threw me");
    }
}

QR: Inline image 2