How to debug "The type initializer for 'my class' threw an exception"

I am getting the exception: The type initializer for 'my class' threw an exception. in my browser after running my web application. Since this seems to be an error message generated from the view (.aspx), there is no way I can see the stack trace or any log for the source of this error.

I have read a bit around the net and one solution to debugging is to throw a TypeInitializationException and then looking at the inner exception to find out what was wrong. How can I do this when I don't know where to surround code with a try/catch ?

Upvotes: 11

Views: 36688

Answers (5)

bobt
bobt

Reputation: 364

Tip: if you have multiple static constructors within the same class that errored out, either put breakpoint on the first static constructors or on all of them.

The unusual thing about static constructor is that when there's issue during construction, it doesn't error out at the point where the construction happens. It errors out later, at the point when the object that was statically constructed is being called/used.

For example, in my case: I have 3 static constructors: A, B, C. Program errors out and paused at a point when value of C is accessed. I put breakpoint on the static constructor for C and the breakpoint didn't get hit. What happens?

Turns out static constructor for B is bad and so the static construction of C doesn't happen. It's confusing because one would think that the error should have popped up as soon as the construction of B failed.

Upvotes: 0

Kevin M
Kevin M

Reputation: 5506

Finally I found reason for this issue is AppConfig settings of my project. Yes, I have two C# projects, Project1 and Project2.

Project1 is contains the Static class MyDetails

public static MyDetails
{
  public static int _LogLevel = Int32.Parse(ConfigurationManager.AppSettings["LogLevel"])

  public static GetData()
   {
     ----code----
     ----code----
   }
}

I have following appConfig settings in Project1

<appSettings>
    <add key="LogLevel" value="5"/>
</appSettings>

The function MyDetails.GetData() is being called From Project2 which is the project I am debugging now. Since Project2 is the target project, the line ConfigurationManager.AppSettings["LogLevel"] will try to read setting LogLevel from Project2. but LogLevel setting is only available in Project1. so, we need to add appsettings in Project2.

The issue The type initializer for threw an exception has been solved after adding the folowing appsettings in appConfig of Project2,

<appSettings>
    <add key="LogLevel" value="5"/>
</appSettings>

Upvotes: 6

petro.sidlovskyy
petro.sidlovskyy

Reputation: 5103

This exception is thrown when the static constructor of 'my class' crashes. Please put your breakpoint there.

Upvotes: 1

spender
spender

Reputation: 120508

This can be caused by a bad static constructor, or by bad inline initialization of static properties/fields. For instance:

class A
{
    static A()
    {
        //buggy code here
    }
    static SomeField f = new ThisClassThrowsWhenConstructed(); // <-- or here
}

Upvotes: 16

Thanatos
Thanatos

Reputation: 1186

There is something that when wrong in your static constructor of the class, and instead of throwing that error, the CLR will throw TypeInitializationException.

Upvotes: 0

Related Questions