简体   繁体   English

Exception.ToString() 抛出异常

[英]Exception.ToString() throws exception

I am trying to log an exception using the Exception.ToString() method.我正在尝试使用Exception.ToString()方法记录异常。 However, I get a new exception from the ToString() method - it seems to originate from the stack trace handling.但是,我从ToString()方法中得到了一个新异常 - 它似乎源自堆栈跟踪处理。 The original error is a FileNotFoundException.原始错误是 FileNotFoundException。 Here is the output:这是输出:

The process was terminated due to an unhandled exception.
Exception Info: System.IO.FileNotFoundException
   at System.Signature.GetSignature(Void*, Int32, System.RuntimeFieldHandleInternal, System.IRuntimeMethodInfo, System.RuntimeType)
   at System.Reflection.RuntimeMethodInfo.FetchNonReturnParameters()
   at System.Reflection.RuntimeMethodInfo.GetParameters()
   at System.Diagnostics.StackTrace.ToString(TraceFormat)
   at System.Environment.GetStackTrace(System.Exception, Boolean)
   at System.Exception.GetStackTrace(Boolean)
   at System.Exception.get_StackTrace()
   at System.IO.FileNotFoundException.ToString()
   at InSQLMDASDriver.InSQLMDASDriver.Init(System.String, System.String)
   at InSQLMDASDriver.InSQLMDASDriverLogic.InSQLMDASDriverLogicInit(System.String, System.String)
   at InSQLMDASDriver.InSQLMDASDriverLogic..ctor(System.String, System.String)
   at InSQLMDASDriverWCFServer.Service1.MainTread()
   at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.ThreadHelper.ThreadStart()

When I run this code I can verify that the exception is thrown from Exception.ToString():当我运行这段代码时,我可以验证异常是从 Exception.ToString() 抛出的:

    private void Init(string defaultWindowName, string mainPath)
    {    
       try 
       {

            // code that fails

        }
        catch(FileNotFoundException e)
        {
             string errorAsString = GetErrorAsString(e);

             Logger.Log(string.Format("Init error at line block {0}: {1}", initBlockCounter, errorAsString), level: LogLevel.Error);


             throw new Exception("FileNotFoundException: " + e.FileName + ", " + e.FusionLog, e);
       }
       catch (Exception e)
       {
              string errorAsString = GetErrorAsString(e);

              Logger.Log(string.Format("Init error at line block {0}: {1}", initBlockCounter, errorAsString), level: LogLevel.Error);


          throw;
      }
}

    string GetErrorAsString(Exception e)
    {
         try
         {
             return e.ToString();
         }
         catch(Exception ne)
         {
                     return e.Message + " (ERROR getting stacktrace: " + ne.Message + ")";
          }
    }

Why does this happen..?为什么会发生这种情况..?

Here is a description that matches my issue where an assembly failing to load causes a FileNotFoundException, and calling ToString() gives another exception:这是与我的问题相匹配的描述,其中程序集无法加载导致 FileNotFoundException,并且调用 ToString() 给出另一个异常:

Here is what happens.这是发生的事情。 The Newtonsoft.Json.JsonConvert.DeserializeObject needs System.Runtime.Serialization.Primitives assembly. Newtonsoft.Json.JsonConvert.DeserializeObject 需要 System.Runtime.Serialization.Primitives 程序集。 But it is not present, so it tries to throw the FileNotFoundException.但它不存在,所以它试图抛出 FileNotFoundException。 It creates the exception object and then it wants to set the stack trace for it.它创建异常对象,然后它想为它设置堆栈跟踪。 So it looks at the topmost frame of the exception stack trace, finds the Newtonsoft.Json.JsonConvert.DeserializeObject method there and tries to use reflection to get its parameters info.所以它查看异常堆栈跟踪的最顶层框架,在那里找到 Newtonsoft.Json.JsonConvert.DeserializeObject 方法并尝试使用反射来获取其参数信息。 When looking at the 2nd parameter of the method which is of type Newtonsoft.Json.JsonSerializerSettings, it tries to build the metadata structure for this parameter type.在查看 Newtonsoft.Json.JsonSerializerSettings 类型的方法的第二个参数时,它会尝试为此参数类型构建元数据结构。 And accidentally, one of the fields of this type is of the StreamingContext type that comes from the System.Runtime.Serialization.Primitives assembly.意外的是,这种类型的字段之一是来自 System.Runtime.Serialization.Primitives 程序集的 StreamingContext 类型。 To get details on the type, it attempts to load this assembly - and since it doesn't exist (that was the reason for the original exception we are trying to build stack trace for), it throws an exception and the result is what you can see.要获取有关类型的详细信息,它会尝试加载此程序集 - 由于它不存在(这是我们尝试为其构建堆栈跟踪的原始异常的原因),它会引发异常,结果就是您可以看到。

See the bug report and discussion there: https://github.com/dotnet/runtime/issues/5203 .请参阅那里的错误报告和讨论: https : //github.com/dotnet/runtime/issues/5203 It should be fixed meanwhile.应该同时修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM