我试图捕获异常的内部异常并将其记录到SQL数据库表中。我已捕获的代码并将SQL异常的异常记录到SQL表中,但即使异常消息显示“查看内部异常以获取详细信息”,我也永远不会获得内部异常记录。就捕获内部异常而言,有人能指出我正确的方向吗?以下是捕获异常的代码,但不适用于内部异常。
try
{
context.CalculateFreight(xmldoc);
}
catch (Exception ex)
{
if (ex.Message.ToString().Length > 2048)
{
ApplicationError myError = new ApplicationError();
myError.ErrorNumber = 1;
myError.ErrorSeverity = 1;
myError.ErrorState = 1;
myError.ErrorLine = 29;
myError.ErrorProcedure = "Web Service - ProcessShipping()";
myError.ErrorMessage = ex.Message.ToString().Substring(0, 2047);
myError.ErrorDateTime = DateTime.Now;
context.ApplicationErrors.AddObject(myError);
context.SaveChanges();
}
else
{
ApplicationError myError = new ApplicationError();
myError.ErrorNumber = 1;
myError.ErrorSeverity = 1;
myError.ErrorState = 1;
myError.ErrorLine = 29;
myError.ErrorProcedure = "Web Service - ProcessShipping()";
myError.ErrorMessage = ex.Message.ToString();
myError.ErrorDateTime = DateTime.Now;
context.ApplicationErrors.AddObject(myError);
context.SaveChanges();
}
if (ex.InnerException.Message.ToString().Length > 0)
{
if (ex.InnerException.Message.ToString().Length > 2048)
{
ApplicationError myInnerException = new ApplicationError();
myInnerException.ErrorNumber = 1;
myInnerException.ErrorSeverity = 1;
myInnerException.ErrorState = 1;
myInnerException.ErrorLine = 29;
myInnerException.ErrorProcedure = "Web Service - INNER EXCEPTION";
myInnerException.ErrorMessage = ex.InnerException.Message.ToString().Substring(0, 2047);
myInnerException.ErrorDateTime = DateTime.Now;
context.ApplicationErrors.AddObject(myInnerException);
context.SaveChanges();
}
else
{
ApplicationError myInnerException = new ApplicationError();
myInnerException.ErrorNumber = 1;
myInnerException.ErrorSeverity = 1;
myInnerException.ErrorState = 1;
myInnerException.ErrorLine = 29;
myInnerException.ErrorProcedure = "Web Service - INNER EXCEPTION";
myInnerException.ErrorMessage = ex.InnerException.Message.ToString();
myInnerException.ErrorDateTime = DateTime.Now;
context.ApplicationErrors.AddObject(myInnerException);
context.SaveChanges();
}
}
}
答案 0 :(得分:1)
如果没有内部异常,则没有内部异常。
从这里开始:How to: Check an Exception's Inner Exception
如果没有原始错误,则InnerException的值为a null引用或Visual Basic中的Nothing。
消息“有关详细信息,请参阅内部异常”。可能是某人的样板文字并不总是适用。