c# 如何阻止异常冒泡
c# how to stop exception from bubbling up
当我的控制器中抛出异常时,操作会一次又一次地执行,当抛出错误时,此循环将继续。
try
{
LogManager.Info("Call:" + invocation.TargetType.Name + "::" + invocation.Method.Name);
var startTime = DateTime.Now;
invocation.Proceed();
var endTime = DateTime.Now;
LogManager.Info("CallEnd:" + invocation.TargetType.Name + "::" + invocation.Method.Name + " Execution Time(Milliseconds): " + (endTime - startTime).Milliseconds);
}
catch (Exception ex)
{
//HttpContext.Current.Request.Abort();
var builder = new StringBuilder();
var dataSource = ConfigurationManager.ConnectionStrings["dbMain"].ToString().Split(';')[0];
builder.AppendLine();
builder.AppendLine(string.Concat(Enumerable.Repeat(">", 50)));
builder.AppendLine("Server Time:-" + DateTime.Now);
//builder.AppendLine("Requested Url:" + HttpContext.Current.Request.Url);
builder.AppendLine(dataSource);
builder.AppendLine("Error at " + invocation.TargetType.Name + "::" + invocation.Method.Name + "(" + JsonConvert.SerializeObject(invocation.Arguments, Formatting.Indented) + ")");
builder.AppendLine(string.Concat(Enumerable.Repeat("-", 50)));
builder.AppendLine(JsonConvert.SerializeObject(ex, Formatting.Indented).Replace("\n", System.Environment.NewLine));
builder.AppendLine(string.Concat(Enumerable.Repeat("<", 50)));
LogManager.Error(builder.ToString());
LogManager.SendMail(ex, builder.ToString());
throw;
}
如果您希望异常停止冒泡,请不要重新抛出它。
在您的代码中有:
"throw ;" 行。删除它。
当我的控制器中抛出异常时,操作会一次又一次地执行,当抛出错误时,此循环将继续。
try
{
LogManager.Info("Call:" + invocation.TargetType.Name + "::" + invocation.Method.Name);
var startTime = DateTime.Now;
invocation.Proceed();
var endTime = DateTime.Now;
LogManager.Info("CallEnd:" + invocation.TargetType.Name + "::" + invocation.Method.Name + " Execution Time(Milliseconds): " + (endTime - startTime).Milliseconds);
}
catch (Exception ex)
{
//HttpContext.Current.Request.Abort();
var builder = new StringBuilder();
var dataSource = ConfigurationManager.ConnectionStrings["dbMain"].ToString().Split(';')[0];
builder.AppendLine();
builder.AppendLine(string.Concat(Enumerable.Repeat(">", 50)));
builder.AppendLine("Server Time:-" + DateTime.Now);
//builder.AppendLine("Requested Url:" + HttpContext.Current.Request.Url);
builder.AppendLine(dataSource);
builder.AppendLine("Error at " + invocation.TargetType.Name + "::" + invocation.Method.Name + "(" + JsonConvert.SerializeObject(invocation.Arguments, Formatting.Indented) + ")");
builder.AppendLine(string.Concat(Enumerable.Repeat("-", 50)));
builder.AppendLine(JsonConvert.SerializeObject(ex, Formatting.Indented).Replace("\n", System.Environment.NewLine));
builder.AppendLine(string.Concat(Enumerable.Repeat("<", 50)));
LogManager.Error(builder.ToString());
LogManager.SendMail(ex, builder.ToString());
throw;
}
如果您希望异常停止冒泡,请不要重新抛出它。 在您的代码中有: "throw ;" 行。删除它。