让构造函数抛出异常是一种好的做法吗?例如,我有一个Person
类,并且age
是它唯一的属性。现在我将这个类提供为
class Person{
int age;
Person(int age) throws Exception{
if (age<0)
throw new Exception("invalid age");
this.age = age;
}
public void setAge(int age) throws Exception{
if (age<0)
throw new Exception("invalid age");
this.age = age;
}
}
https://stackoverflow.com/questions/6086334
复制相似问题