How to debug "Type initializer for" my class "threw an exception"

I get an exception: The type initializer for 'my class' threw an exception.in my browser after starting my web application. Since this seems to be an error message generated from a view (.aspx), I cannot see the stack trace or any log for the source of this error.

I read the network a bit, and one debugging solution is to throw it away TypeInitializationExceptionand then look at the internal exception to find out what was wrong. How to do this when I do not know where to surround the code with try / catch?

+5
source share
4 answers

/. :

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

, - AppConfig . , #, Project1 Project2.

Project1 Static MyDetails

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

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

appConfig Project1

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

MyDetails.GetData() Project2, , . Project2 , ConfigurationManager.AppSettings [ "LogLevel" ] LogLevel Project2. LogLevel Project1. appsettings Project2.

. appConfig Project2,

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

"my class". , .

+1
source

There is something that is not right in your static class constructor, and instead of throwing this error, it CLRwill throw TypeInitializationException.

0
source

All Articles