【问题标题】:Exception.ToString() throws exceptionException.ToString() 抛出异常
【发布时间】:2017-06-06 10:27:15
【问题描述】:

我正在尝试使用Exception.ToString() 方法记录异常。但是,我从ToString() 方法中得到了一个新异常——它似乎源于堆栈跟踪处理。 原始错误是 FileNotFoundException。这是输出:

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()

当我运行这段代码时,我可以验证异常是从 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 + ")";
          }
    }

为什么会发生这种情况..?

【问题讨论】:

  • 你能告诉我们调用GetErrorAsString的代码吗?
  • 你能证明这个例外吗?但乍一看,这可能是由于 e 对象为空。检查一下。
  • @RenatoAfonso - 如果是这种情况,e.Messagecatch(Exception ne) 部分中的引用将导致再次重新抛出......
  • 根据堆栈跟踪,您的代码永远不会到达GetErrorAsString()Exception.ToString() 在任何情况下都不会抛出 FileNotFoundException
  • 我猜运行时正在尝试加载在创建堆栈跟踪期间找不到的程序集。你能告诉我们 FileNotFoundException 的消息吗?

标签: c# .net


【解决方案1】:

这是一个与我的问题相匹配的描述,其中程序集加载失败导致 FileNotFoundException,调用 ToString() 会产生另一个异常:

这就是发生的事情。这 Newtonsoft.Json.JsonConvert.DeserializeObject 需要 System.Runtime.Serialization.Primitives 程序集。但它不是 存在,因此它尝试抛出 FileNotFoundException。它创建 异常对象,然后它想为其设置堆栈跟踪。 所以它查看异常堆栈跟踪的最顶层帧,发现 那里的 Newtonsoft.Json.JsonConvert.DeserializeObject 方法和 尝试使用反射来获取其参数信息。看的时候 方法的第二个参数类型 Newtonsoft.Json.JsonSerializerSettings,它尝试构建元数据 此参数类型的结构。意外地,其中一个领域 此类型的 StreamingContext 类型来自 System.Runtime.Serialization.Primitives 程序集。获取详细信息 类型,它会尝试加载这个程序集 - 因为它没有 存在(这就是我们试图解决的原始异常的原因 建立堆栈跟踪),它抛出一个异常,结果是什么 你可以看到。

查看错误报告和讨论:https://github.com/dotnet/runtime/issues/5203。应该同时修复。

【讨论】:

  • 请在此处总结链接
猜你喜欢
  • 2012-08-19
  • 2013-05-24
  • 2011-05-30
  • 1970-01-01
  • 2011-02-25
  • 2012-01-24
  • 1970-01-01
相关资源
最近更新 更多