處理 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM