Java 在类中抛出异常
我在读一些关于创建不可变类的书,我注意到它们使用私有方法来检查以确保传递的参数确实正确,如下所示:Java 在类中抛出异常,java,Java,我在读一些关于创建不可变类的书,我注意到它们使用私有方法来检查以确保传递的参数确实正确,如下所示: private void check(int red, int green, int blue) { if (red < 0 || red > 255 || green < 0 || green > 255 || bl
private void check(int red,
int green,
int blue) {
if (red < 0 || red > 255
|| green < 0 || green > 255
|| blue < 0 || blue > 255) {
throw new IllegalArgumentException();
}
}
private void check(内部红色,
int绿色,
(蓝色){
如果(红色<0 | |红色>255
||绿色<0 | |绿色>255
||蓝色<0 | |蓝色>255){
抛出新的IllegalArgumentException();
}
}
我的问题是,我有以下类,但当我抛出异常时,我想向用户指定哪个参数无效。我是否必须为每个参数编写一个私有方法并进行检查
final public class AnonymousCilent {
final private String anonymousCilentID;
final private String anonymousCilentFirstName;
final private String anonymousCilentLastName;
public AnonymousCilent(String anonymousCilentID, String anonymousCilentFirstName, String anonymousCilentLastName) {
this.anonymousCilentID = anonymousCilentID;
this.anonymousCilentFirstName = anonymousCilentFirstName;
this.anonymousCilentLastName = anonymousCilentLastName;
}
/*
private void checkParameter(String check){
if(check == null || check.length() < 0 ){
throw new IllegalArgumentException ("Please ensure all values are provided");
}
}
*/
public String getAnonymousCilentFirstName() {
return anonymousCilentFirstName;
}
public String getAnonymousCilentLastName() {
return anonymousCilentLastName;
}
最终公共类匿名认证{
最终私有字符串匿名cilentid;
最终私有字符串anonymousCilentFirstName;
最终私有字符串anonymousCilentLastName;
public AnonymousCilent(字符串anonymousCilentID、字符串anonymousCilentFirstName、字符串anonymousCilentLastName){
this.anonymousCilentID=anonymousCilentID;
this.anonymousCilentFirstName=anonymousCilentFirstName;
this.anonymousCilentLastName=anonymousCilentLastName;
}
/*
私有void checkParameter(字符串检查){
if(check==null | | check.length()<0){
抛出新的IllegalArgumentException(“请确保提供了所有值”);
}
}
*/
公共字符串getAnonymousCilentFirstName(){
返回anonymousCilentFirstName;
}
公共字符串getAnonymousCilentLastName(){
返回匿名cilentlastname;
}
您不必有单独的方法,但需要区分可能引发异常的不同原因。例如,如果块:
if(red < 0 || red > 255) {
throw new IllegalArgumentException("value for red must be in the range 0-255");
}
if(blue < 0 || blue > 255) {
...
您可以创建一个助手方法:
private String checkNotNullOrEmpty(String s, String name) {
if (s == null || s.isEmpty()) {
throw new IllegalArgumentException(name + " must not be null or empty");
}
return s;
}
然后在构造函数中像这样使用它:
public AnonymousCilent(String anonymousCilentID, String anonymousCilentFirstName, String anonymousCilentLastName,
String gender, Date arrivalDate, String immStatus, Date registrationDate,
String registrationSite, String siteName, String comments) {
this.anonymousCilentID = checkNotNullOrEmpty(anonymousCilentID, "anonymousCilentID");
this.anonymousCilentFirstName = checkNotNullOrEmpty(anonymousCilentFirstName, "anonymousCilentFirstName");
this.anonymousCilentLastName = checkNotNullOrEmpty(anonymousCilentLastName, "anonymousCilentLastName");
// etc.
}
注意,类(Java8)已经包含类似的帮助器方法
此外,该库还有一个名为的类,该类使用类似的helper方法命名
与颜色值的示例类似:
public int checkColorValue(int value, String name) {
if (value < 0 || value > 255) {
throw new IllegalArgumentException(name + " must be between 0 and 255");
}
return value;
}
public Color(int red, int green, int blue) {
this.red = checkColorValue(red, "red");
this.green = checkColorValue(green, "green");
this.blue = checkColorValue(blue, "blue");
}
public int checkColorValue(int值,字符串名称){
如果(值<0 | |值>255){
抛出新的IllegalArgumentException(名称+“必须介于0和255之间”);
}
返回值;
}
公共颜色(红色、绿色、蓝色){
this.red=checkColorValue(红色,“红色”);
this.green=checkColorValue(绿色,“绿色”);
this.blue=checkColorValue(蓝色,“蓝色”);
}
您有两种选择,在我看来,这两种选择都不如手动操作简单
如果使用规则注释类数据成员并使用验证器验证输入,则可以使用框架。可以使用hibernate执行此操作:
(适用于非常简单的规则)
如果你有非常复杂的业务逻辑和其他东西,你可以做一个非常繁重的解决方案,并嵌入一个prolog解释器或其他逻辑引擎,它们有时被称为
99.9%的时间你都没有,你最好在你的视图中使用适当的GUI组件和代码,而不是在代码中出现异常,或者自己编写功能,从而最大限度地减少用户输入无效输入的机会。我发现,让用户始终输入有效输入并确保在GUI中发生,但这并不总是可能的。如果您有多种情况,有时枚举开关状态会产生奇迹:
枚举案例{
有效,
重复名称,
残疾人住址,
发射型计算机断层扫描仪
}
然后使用switch语句来处理每种情况
public int checkColorValue(int value, String name) {
if (value < 0 || value > 255) {
throw new IllegalArgumentException(name + " must be between 0 and 255");
}
return value;
}
public Color(int red, int green, int blue) {
this.red = checkColorValue(red, "red");
this.green = checkColorValue(green, "green");
this.blue = checkColorValue(blue, "blue");
}