Java 缺少重要参数/依赖项时抛出什么异常?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2517965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:44:13  来源:igfitidea点击:

What exception to throw when an important parameter/dependency is missing?

javaoopexception

提问by Pentium10

Take this method

采取这个方法

/**
 * @return List of group IDs the person belongs to
 *
 */
public List<String> getGroups() {
    if (this.getId().equals("")) return null;
}

I would like to throw exception instead returning null, what's the exception to throw when an important parameter/dependency has not been set?

我想抛出异常而不是返回 null,当重要的参数/依赖项尚未设置时抛出的异常是什么?

采纳答案by BalusC

I'd use IllegalArgumentExceptionif the parameter/argument is controlled from outside, or IllegalStateExceptionif the method is just called at a wrong moment (state). In your specific case I think it's the latter. A (dubious) alternative is NullPointerException.

IllegalArgumentException如果参数/参数是从外部控制的,或者IllegalStateException方法只是在错误的时刻(状态)被调用,我会使用。在你的具体情况下,我认为是后者。一个(可疑的)替代方案是NullPointerException

This should however be explicitly documented in the @throwsso that the user understands the reason.

然而,这应该在 中明确记录,@throws以便用户理解原因。

回答by Stefan Kendall

回答by Arne Burmeister

I would use an IllegalStateException because the id is state of the owner. If the id would have passed as parameter, an IllegalArgumentException would be right.

我会使用 IllegalStateException 因为 id 是所有者的状态。如果 id 将作为参数传递,则 IllegalArgumentException 将是正确的。

回答by Jay

I would create my own Exception type by extending Exception. That way calling functions can catch that particular Exception and handle it gracefully as appropriate. Note, you can do the same thing with just about anything that Extends Exception, but I prefer to create my own Exception classes so I can be very robust in my exception handling. This is, of course, up to you though.

我将通过扩展异常来创建我自己的异常类型。这样调用函数就可以捕获那个特定的异常并在适当的时候优雅地处理它。请注意,您几乎可以对任何扩展异常的东西做同样的事情,但我更喜欢创建自己的异常类,这样我就可以在异常处理中非常健壮。当然,这取决于你。

回答by Sam Holder

If its not possible to ensure that the id is always set (by requiring it in the constructor for example, where you could check that a valid id has been passed) then I think the other suggestions to throw IllegalStateExceptionare correct. But it would be better to try and ensure that your object can't get into this state in the first place if at all possible

如果无法确保始终设置 id(例如,通过在构造函数中要求它,您可以检查是否已传递有效 id),那么我认为抛出IllegalStateException的其他建议是正确的。但是最好尝试确保您的对象一开始就无法进入这种状态,如果可能的话

回答by aephyx

Instead of throwing an exception, you should just return an empty list. If a dependency/parameter isn't met, then there are no results. From the comments and code posted, it looks like that's the expected behavior. If the id is empty, then there are no groups attached, thus an empty list.

您应该只返回一个空列表,而不是抛出异常。如果不满足依赖项/参数,则没有结果。从发布的评论和代码来看,这似乎是预期的行为。如果 id 为空,则没有附加组,因此是一个空列表。