我应该使用哪个Java异常?

时间:2022-09-10 23:10:08

Let's assume I'm a complete lazy bum and I don't want to invest the several dozen keystrokes needed for my own exception class (it's not utterly important which gets used, really). However, to pretend I'm following good practices here, I want a pre-existing one that best fits my situation.

让我们假设我是一个完全懒惰的流浪汉,我不想投入我自己的异常类所需的几十个击键(它真的不是非常重要的使用它)。但是,假装我在这里遵循良好做法,我想要一个最符合我情况的预先存在的做法。

Problem: I need to throw an exception when my class's constructor receives an object in its parameters that is not found within a given list I've built elsewhere.

问题:当我的类的构造函数在其参数中接收到一个在我在别处构建的给定列表中找不到的对象时,我需要抛出一个异常。

Which exception class would be appropriate to throw for that?

哪个异常类适合抛出?

5 个解决方案

#1


20  

IllegalArgumentException

抛出:IllegalArgumentException

#2


12  

Winner by Accuracy: java.lang.IllegalArgumentException

Accuracy的获胜者:java.lang.IllegalArgumentException

#3


10  

IllegalArgumentException is indeed the answer here, but I'd say you have a problem with your design. In essence, your class invariant is dependent on the state of some external object, which is a violation of encapsulation. There's no way to determine whether a call to your constructor will succeed without knowledge of some other object, which leads to a confusing and easily misused API.

IllegalArgumentException确实是答案,但我会说你的设计有问题。本质上,您的类不变量依赖于某个外部对象的状态,这违反了封装。在没有其他对象知识的情况下,无法确定对构造函数的调用是否会成功,从而导致混淆且容易被误用的API。

This problem is mitigated somewhat if the list you refer to is a static final unmodifiable List (see java.util.Collections.unmodifiableList()) and contained within the class in question, but I still don't like it terribly much. Better is to encapsulate, if possible, the acceptable parameter values in an enum, which will eliminate the need for an exception altogether. I generally dislike exceptions thrown from constructors. If you must throw an exception, use a factory method instead.

如果您引用的列表是静态的最终不可修改的List(请参阅java.util.Collections.unmodifiableList())并包含在相关类中,则此问题会有所缓解,但我仍然不太喜欢它。如果可能的话,更好的是在枚举中封装可接受的参数值,这将完全消除对异常的需要。我通常不喜欢构造函数抛出的异常。如果必须抛出异常,请改用工厂方法。

If an option is not available to you that eliminates the need for an external list, you may need to rethink your design.

如果您无法使用某个选项而无需外部列表,则可能需要重新考虑您的设计。

#4


0  

If you don't fear an explosion in the number of classes, you can extend the IllegalArgumentException for this situation.

如果您不担心类数量的爆炸,可以针对这种情况扩展IllegalArgumentException。

 public class InvalidInstance extends IllegalArgumentException{
    private String[] parameter;

    public InvalidInstance (String[] param){
        this.parameter = param;
    }

    public String getMessage()
    String msg = "YOUR_MESSAGE";
    /* I think a string as "The currente object is
       invalid for parameter "+cycle for over parameter;*/
    msg += super.geTMessage();
    return msg;
}

public Constructor(parameter1,...){
    String[] param = new String[number_parameters]
    if...
    throws new InvalidInstance(param);
}

In this way, you can log all the parameters what run the exception.

通过这种方式,您可以记录运行异常的所有参数。

This code isn't very beautiful to read: you can use if you prefer the very structured code. A simple IllegalArgumentException is more common :)

这段代码读起来不是很漂亮:如果您喜欢非常结构化的代码,可以使用它。一个简单的IllegalArgumentException更常见:)

#5


-2  

Just in case you didn't get it, IllegalArgumentException :)

万一你没有得到它,IllegalArgumentException :)

#1


20  

IllegalArgumentException

抛出:IllegalArgumentException

#2


12  

Winner by Accuracy: java.lang.IllegalArgumentException

Accuracy的获胜者:java.lang.IllegalArgumentException

#3


10  

IllegalArgumentException is indeed the answer here, but I'd say you have a problem with your design. In essence, your class invariant is dependent on the state of some external object, which is a violation of encapsulation. There's no way to determine whether a call to your constructor will succeed without knowledge of some other object, which leads to a confusing and easily misused API.

IllegalArgumentException确实是答案,但我会说你的设计有问题。本质上,您的类不变量依赖于某个外部对象的状态,这违反了封装。在没有其他对象知识的情况下,无法确定对构造函数的调用是否会成功,从而导致混淆且容易被误用的API。

This problem is mitigated somewhat if the list you refer to is a static final unmodifiable List (see java.util.Collections.unmodifiableList()) and contained within the class in question, but I still don't like it terribly much. Better is to encapsulate, if possible, the acceptable parameter values in an enum, which will eliminate the need for an exception altogether. I generally dislike exceptions thrown from constructors. If you must throw an exception, use a factory method instead.

如果您引用的列表是静态的最终不可修改的List(请参阅java.util.Collections.unmodifiableList())并包含在相关类中,则此问题会有所缓解,但我仍然不太喜欢它。如果可能的话,更好的是在枚举中封装可接受的参数值,这将完全消除对异常的需要。我通常不喜欢构造函数抛出的异常。如果必须抛出异常,请改用工厂方法。

If an option is not available to you that eliminates the need for an external list, you may need to rethink your design.

如果您无法使用某个选项而无需外部列表,则可能需要重新考虑您的设计。

#4


0  

If you don't fear an explosion in the number of classes, you can extend the IllegalArgumentException for this situation.

如果您不担心类数量的爆炸,可以针对这种情况扩展IllegalArgumentException。

 public class InvalidInstance extends IllegalArgumentException{
    private String[] parameter;

    public InvalidInstance (String[] param){
        this.parameter = param;
    }

    public String getMessage()
    String msg = "YOUR_MESSAGE";
    /* I think a string as "The currente object is
       invalid for parameter "+cycle for over parameter;*/
    msg += super.geTMessage();
    return msg;
}

public Constructor(parameter1,...){
    String[] param = new String[number_parameters]
    if...
    throws new InvalidInstance(param);
}

In this way, you can log all the parameters what run the exception.

通过这种方式,您可以记录运行异常的所有参数。

This code isn't very beautiful to read: you can use if you prefer the very structured code. A simple IllegalArgumentException is more common :)

这段代码读起来不是很漂亮:如果您喜欢非常结构化的代码,可以使用它。一个简单的IllegalArgumentException更常见:)

#5


-2  

Just in case you didn't get it, IllegalArgumentException :)

万一你没有得到它,IllegalArgumentException :)