在类中抛出异常

时间:2015-07-03 19:59:03

标签: java

我正在做一些关于创建不可变类的阅读here,我注意到他们使用私有方法来检查以确保传递的参数确实是正确的,如下所示:

 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();
        }
    } 

我的问题是,我有以下类,但是当我抛出异常时,我想指定用户哪个参数无效。我是否必须为每个参数编写一个私有方法并检查它?

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;
    }

3 个答案:

答案 0 :(得分:2)

您可以创建辅助方法:

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.
}

请注意,类java.util.Objects(Java 8)已包含类似的辅助方法。

此外,Google Guava库还有一个名为Preconditions的类,其中包含类似的辅助方法。

与颜色值的示例类似:

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");
}

答案 1 :(得分:1)

您不必拥有单独的方法,但需要将可能抛出异常的不同原因分开。例如,您可能有多个if块:

if(red < 0 || red > 255) {
    throw new IllegalArgumentException("value for red must be in the range 0-255");
}

if(blue < 0 || blue > 255) {
...

请注意,根据您的程序是否已经足够复杂以使用外部库,使用JSR-303验证API可能更有意义:

public final class ImmutableClass {

    @NotEmpty
    final String clientID;

    @Min(0)
    @Max(255)
    final int red;
}

答案 2 :(得分:0)

你有两个选择,在我看来,这两种选择都不比手动操作容易。

如果您使用规则注释类数据成员并使用验证程序验证输入,则可以使用框架。你可以用hibernate做到这一点: http://hibernate.org/validator/documentation/getting-started/(适用于非常简单的规则)

如果你有非常复杂的业务逻辑和其他东西,你可以做一个非常重的解决方案并嵌入一个prolog解释器或其他逻辑引擎,他们有时被称为。 (适用于非常复杂的规则)

99.9%的时间你没有,你最好通过在你的View中使用适当的GUI组件和代码来最小化用户输入无效输入的机会,而不是在代码中出现异常,或者自己编写功能。我发现用户总是输入有效输入并确保在GUI中发生这种情况更容易,但是这并不总是可行的。如果您有多个案例,有时枚举开关语句可以创造奇迹:

枚举CASE {     有效,     DUPLICATENAME,     无效地址,     ECT }

然后有一个switch语句来处理每个case。

相关问题