Yos*_*ari 3 c# validation exception-handling exception
验证用户输入的正确方法(如果有的话)是什么
这一个(首先抛出异常):
private void DisposeWorkFlowItem(WorkFlowItem item)
{
if (item == null)
{
throw new ArgumentException("work flow item must have value");
}
//TO DO: add a call to delete the task from worker service.
_workFlowItems.Remove(item);
_workFlowItemsStore.Delete(item);
}
Run Code Online (Sandbox Code Playgroud)
或者这个(先做行动):
private void DisposeWorkFlowItem(WorkFlowItem item)
{
if (item != null)
{
//TO DO: add a call to delete the task from worker service.
_workFlowItems.Remove(item);
_workFlowItemsStore.Delete(item);
}
else
{
throw new ArgumentException("work flow item must have value");
}
}
Run Code Online (Sandbox Code Playgroud)
有指导方针吗?
没有真正的指导方针或规则,但第一个通常是首选,因为您可以删除else,删除一个级别的缩进.
private void DisposeWorkFlowItem(WorkFlowItem item)
{
if (item == null)
{
throw new ArgumentException("work flow item must have value");
}
//TO DO: add a call to delete the task from worker service.
_workFlowItems.Remove(item);
_workFlowItemsStore.Delete(item);
}
Run Code Online (Sandbox Code Playgroud)
较少的缩进使代码更易于理解,尤其是在具有多个此类检查的情况下.
哦,当null你检查一个参数时,通常会抛出一个ArgumentNullException参数名作为第一个参数:
throw new ArgumentNullException("item");
Run Code Online (Sandbox Code Playgroud)