分享NET代码获取InnerException内容

2024/04/16

问题:

  • C#代码执行发现异常里InnerException是null(Visual Studio调试是可以看到InnerException有值的)

解决方法:

  • 在stackoverflow找到到答案:

The trick is to recognize the type of exception being thrown and cast the General Exception to the correct Type where you will then have access to extended properties for that Exception type.

大致意思:诀窍在于识别被抛出的异常的类型,并将一般异常转换为正确的类型,这样你就可以访问该异常类型的扩展属性了。

Demo:

 catch (UFSoft.UBF.Business.BusinessException e)
 {
     throw new Exception("RCV:" + entity.DocNo + "审核失败:" + e.InnerException.Message);
 }