在测试最终用户插入的参数时使用异常或流控制更好吗?伪代码:
class A{
List listOfPeople;
public void insertName(String name)
{
//if the name is not inserted in the registry it throws a runtime exception
throw new IllegalArgumentException("Please insert a registered name");
}
}
或者最好执行以下操作:
class A{
List listOfPeople;
public void insertName(String name)
{
if (!listOfPeople.contains(name){
System.out.println("name not in the list, please insert a registered name");
reInsertName();
}
else
..do stuff..
}
}
我读过抛出异常非常昂贵。这是否意味着只要有可能不抛出它们,我们就必须避免抛出它们试图控制问题?
谢谢,确实是