
The type initializer for 'Example' threw an exception. Unhandled Exception: System.TypeInitializationException: The example displays the following output:

If (value 1) throw new ArgumentOutOfRangeException() Private static TestClass test = new TestClass(3) Since this exception is not handled, it is wrapped in a TypeInitializationException exception. As a result, the TestClass class constructor throws an ArgumentOutOfRangeException. That value, however, is illegal only values of 0 or 1 are permitted. The Example class includes a static (in C#) or Shared (in Visual Basic) field of type TestClass that is instantiated by passing a value of 3 to its class constructor. The following example shows a TypeInitializationException exception thrown by a compiler-generated static constructor. IL_0006: stsfld class TestClass Example::test

IL_0001: newobj instance void TestClass.ctor(int32) method private specialname rtspecialname static For instance, when the C# and VB compilers compile the following example, they generate the IL for a static constructor that is similar to this: You can inspect it by using a utility such as IL Disassembler. In this case, the language compiler generates a static constructor for the type. The type has static (in C#) or Shared (in Visual Basic) variables that are declared and initialized in a single statement. It has been explicitly defined as a member of a type. A static constructor exists in a type if: What often makes this exception difficult to troubleshoot is that static constructors are not always explicitly defined in source code. If an exception is thrown in a static constructor, that exception is wrapped in a TypeInitializationException exception, and the type cannot be instantiated. Some of the more common causes of a TypeInitializationException exception are:Īn unhandled exception in a static constructor The InnerException property indicates why the static constructor was unable to instantiate the type. Most commonly, a TypeInitializationException exception is thrown when a static constructor is unable to instantiate a type. For more information on static constructors, see Static Constructors. If a static constructor is not explicitly defined, compilers automatically create one to initialize any static (in C#) or Shared (in Visual Basic) members of the type. Static constructors can be explicitly defined by a developer. Static constructors and the TypeInitializationExceptionĪ static constructor, if one exists, is called automatically by the runtime before creating a new instance of a type. Regular expression match timeout values.Static constructors and the TypeInitializationException exception.The following sections describe some of the situations in which a TypeInitializationException exception is thrown. TypeInitializationException uses the HRESULT COR_E_TYPEINITIALIZATION, which has the value 0x80131534.įor a list of initial property values for an instance of TypeInitializationException, see the TypeInitializationException constructors. Instead, the cause of the exception should be investigated and eliminated. Consequently, other than possibly for troubleshooting debug code, the exception should not be handled in a try/ catch block. Most commonly, the TypeInitializationException is thrown in response to some change in the executing environment of the application.

Typically, the TypeInitializationException exception reflects a catastrophic condition (the runtime is unable to instantiate a type) that prevents an application from continuing. The InnerException property of TypeInitializationException holds the underlying exception. When a class initializer fails to initialize a type, a TypeInitializationException is created and passed a reference to the exception thrown by the type's class initializer. SerializableAttribute ComVisibleAttribute Remarks Inherits Exception Public NotInheritable Class TypeInitializationException Inherit SystemException Public NotInheritable Class TypeInitializationException Inherit Exception type TypeInitializationException = class Public sealed class TypeInitializationException : SystemException type TypeInitializationException = class Public sealed class TypeInitializationException : SystemException In this article public ref class TypeInitializationException sealed : Exception public ref class TypeInitializationException sealed : SystemException public sealed class TypeInitializationException : Exception public sealed class TypeInitializationException : SystemException The exception that is thrown as a wrapper around the exception thrown by the class initializer.
