找到原来的异常

问题描述 投票:0回答:1

我有一个导入例程,可以将许多值导入到对象的属性中。 Ojbects 属性中有很多值,它们可以按任何顺序排列,因此我使用反射来查找属性,然后设置其值。这一切都运行得非常好,除非对象属性设置方法引发异常,我看不到它。

代码摘录:

try
  {
  PropertyInfo piInstance = MBtype.GetProperty(columnlist[intColumn]);
  piInstance.SetValue(newMailbox, CurrentRow[intColumn]);
   }
catch (Exception ex)
  {
   HelpFunc.writeToLog($"Error: '{ex.Message}'", "Error");
  }

初始错误代码的属性是:

 throw new ArgumentException($"{value} must be valid email address", value);

但是,当我查看日志时,它没有告诉我参数异常,它告诉我错误是:

“错误:‘调用目标已引发异常。’”

即原来的异常丢失了。setValue是一个Net函数。我怎样才能获得原始异常,以便我可以获取日志记录的详细信息。

非常感谢!

c# exception properties
1个回答
0
投票

你必须检查内部异常:

try
  {
  PropertyInfo piInstance = MBtype.GetProperty(columnlist[intColumn]);
  piInstance.SetValue(newMailbox, CurrentRow[intColumn]);
   }
catch (Exception ex)
  {
   HelpFunc.writeToLog($"Error: '{ex.Message}', Inner Exception: '{ex.InnerException?.Message}'", "Error");
  }

我建议始终记录异常的

ToString()
方法,因为这样你就拥有了所有信息(包括堆栈跟踪和内部异常)。

© www.soinside.com 2019 - 2024. All rights reserved.