我正在使用以下内容来捕获异常类型并添加更多详细信息:
try
{
this.ApplyRules();
return base.SaveChanges();
}
catch (DbEntityValidationException ex)
{
var sb = new StringBuilder();
foreach (var failure in ex.EntityValidationErrors)
{
sb.AppendFormat("{0} failed validation\n", failure.Entry.Entity.GetType());
foreach (var error in failure.ValidationErrors)
{
sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage);
sb.AppendLine();
}
}
throw new DbEntityValidationException(
"Entity Validation Failed - errors follow:\n" +
sb.ToString(), ex
); // Add the original exception as the innerException
}
catch (Exception ex)
{
< some code here that would give me more detail about what the exception was >
throw new Exception( );
}
这可行,但现在我希望能够捕获其他类型异常的 innerException.exceptionMessage 。
有没有一种方法可以向其中添加代码,其中包括所有其他异常类型的最低级别的 innerException.exceptionMessage。一些会沿着异常树走并获取最终消息的代码?