TypeInitializationException Class

Represents errors that occur during the initialization of a type.

Syntax

public sealed class TypeInitializationException : SystemException

Constructors

TypeInitializationException()

public TypeInitializationException()

Initializes a new instance of the TypeInitializationException class.

TypeInitializationException(string message)

public TypeInitializationException(stringmessage)

Initializes a new instance of the TypeInitializationException class with a specified error message.

TypeInitializationException(string message, Exception innerException)

public TypeInitializationException(stringmessage, ExceptioninnerException)

Initializes a new instance of the TypeInitializationException class with a specified error message and a reference to the inner exception that is the cause of this exception.

Remarks

A TypeInitializationException is thrown when an error occurs during the static initialization of a type. This typically happens when code in a static constructor, or code that is accessed by a static constructor, throws an exception.

The InnerException property contains the exception that was thrown during the type initialization. If the InnerException is not specified, the Message property is a null reference (Nothing in Visual Basic). If the InnerException is not specified, the Message property is set to the default message.

Example

C# Example

using System;

public class FaultyInitializer
{
    static FaultyInitializer()
    {
        throw new InvalidOperationException("Initialization failed!");
    }
}

public class Program
{
    public static void Main()
    {
        try
        {
            var instance = new FaultyInitializer();
        }
        catch (TypeInitializationException ex)
        {
            Console.WriteLine("A TypeInitializationException occurred.");
            Console.WriteLine($"Message: {ex.Message}");
            if (ex.InnerException != null)
            {
                Console.WriteLine($"Inner Exception: {ex.InnerException.Message}");
            }
        }
    }
}

Requirements

Namespace: System

Assembly: mscorlib.dll (in .NET Framework 1.0)

Assembly: System.Runtime.dll (in .NET Core, .NET 5+)