处理 InnerException 最佳方案?

如何获取 innerException 内部错误信息

String innerMessage = (ex.InnerException != null) 
                      ? ex.InnerException.Message
                      : "";

这断代码看上去可以解决问题,但是党innerException中还有innerException的得时候就无效了。

所以有了下面得扩展方法,使用递归获取innerException,完美解决

public static class ExceptionExtensions
{
    public static Exception GetOriginalException(this Exception ex)
    {
        if (ex.InnerException == null) return ex;

        return ex.InnerException.GetOriginalException();
    }
}

参考:http://stackoverflow.com/questions/1456563/best-way-to-check-for-inner-exception

原文地址:https://www.cnblogs.com/miralce/p/4892193.html