【发布时间】:2010-08-10 09:02:57
【问题描述】:
public bool AddEntity(int parentId, 字符串描述) { 尝试 { _connection.Open(); SqlCommand command = new SqlCommand("INSERT Structure (Path,Description)" + "VALUES(" + GetPath(parentId) + ".GetDescendant(" + GetLastChildPath(parentId, 1) + ", NULL), " + 描述 + ")", _connection);
if (command.ExecuteNonQuery() <= 0) _success = false;
command.Connection.Close();
if (_success)
{
return true;
}
throw new Exception("An error has occured whilst trying to add a entity");
}
catch (Exception ex)
{
AddError(new ErrorModel("An error has occured whilst trying to add a entity", ErrorHelper.ErrorTypes.Critical, ex));
return false;
}
}
有没有更好的方法来处理上例中的异常?
提前感谢您的帮助。
克莱尔
【问题讨论】:
-
除了 Rob Stevenson-Leggetts 的回答之外,我还会让 catch-exception 更加具体。例如首先捕获 SqlException,因为这将包含有关实际错误和堆栈跟踪的更多具体信息。将 catch(Exception ex) 保留为最后一个 catch-block。
标签: model-view-controller asp.net-mvc-2 exception-handling