This program shows that this exception is raised when any exception is thrown inside a type initializer, also called a static constructor.
Info The runtime wraps any exception raised in the static constructor inside a TypeInitializationException.
And The InnerException is the original cause of the problem. The TypeInitializationException is only raised from type initializers.
//// Static constructor for the program class.// ... Also called a type initializer.// ... It throws an exception in runtime.//
int number = 100;
int denominator = int.Parse("0");
int result = number / denominator;
static void Main()
// Entry point.
}Unhandled Exception: System.TypeInitializationException: The type initializer for
'Program' threw an exception. --->
System.DivideByZeroException: Attempted to divide by zero.
--- End of inner exception stack trace ---
Notes, program. When Main is reached, the static Program constructor is executed. The code in the static constructor attempts to divide by zero.
And This causes a DivideByZeroException to be thrown. The runtime then wraps this exception inside a TypeInitializationException.
Fix. To fix this error, you can add try-catch blocks around the body of the static constructor that throws it. Alternatively, you could avoid the exception with an if-statement.