如何獲取 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