处理 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();
    }
}

posted on 2022-11-22 14:44  itjeff  阅读(505)  评论(0编辑  收藏  举报

导航