我使用java语言,我有一个方法,如果它被找到,应该返回一个对象。

如果没有找到,我是否应该:

返回null 抛出异常 其他

哪一个是最好的实践或习语?


当前回答

例外情况与契约式设计有关。

一个对象的接口实际上是两个对象之间的契约,调用方必须满足契约,否则接收方可能会异常失败。有两种可能的契约

1)所有输入的方法都是有效的,在这种情况下,当对象没有找到时必须返回null。

2)只有某些输入是有效的,即导致找到对象的输入。在这种情况下,您必须提供第二个方法,允许调用者确定其输入是否正确。例如

is_present(key)
find(key) throws Exception

当且仅当你提供了第二个契约的两个方法,你被允许抛出异常,但什么都没有发现!

其他回答

返回空值而不是抛出异常,并在API文档中清楚地记录空返回值的可能性。如果调用代码不遵守API并检查空情况,它很可能会导致某种“空指针异常”:)

在c++中,我可以想到3种不同的方法来设置查找对象的方法。

选择一个

Object *findObject(Key &key);

当找不到对象时返回null。很好很简单。我选这个。下面的替代方法是为那些不讨厌out-params的人准备的。

选项B

void findObject(Key &key, Object &found);

传入一个将接收该对象的变量引用。当找不到对象时,该方法抛出异常。如果不是真的期望找不到对象,那么这种约定可能更适合——因此您可以抛出异常来表示这是一个意外情况。

C选项

bool findObject(Key &key, Object &found);

当找不到对象时,该方法返回false。与选项A相比,这个选项的优点是,您可以在一个明确的步骤中检查错误情况:

if (!findObject(myKey, myObj)) { ...

例外应该是例外。如果返回null是有效的,则返回null。

如果您总是希望找到一个值,那么如果缺少该值则抛出异常。这个例外意味着有问题。

如果该值可以缺失或存在,并且两者都对应用程序逻辑有效,则返回null。

更重要的是:在代码的其他地方做什么?一致性很重要。

抛出异常的好处:

Cleaner control flow in your calling code. Checking for null injects a conditional branch which is natively handled by try/catch. Checking for null doesn't indicate what it is you're checking for - are you checking for null because you're looking for an error you're expecting, or are you checking for null so you don't pass it further on downchain? Removes ambiguity of what "null" means. Is null representative of an error or is null what is actually stored in the value? Hard to say when you only have one thing to base that determination off of. Improved consistency between method behavior in an application. Exceptions are typically exposed in method signatures, so you're more able to understand what edge cases the methods in an application account for, and what information your application can react to in a predictable manner.

有关更多示例的解释,请参见:http://metatations.com/2011/11/17/returning-null-vs-throwing-an-exception/

这取决于方法的性质以及如何使用它。如果正常情况下可能找不到对象,则返回null。如果总是找到对象是正常行为,则抛出异常。

根据经验,只在发生异常时使用异常。不要在编写代码时将异常抛出和捕获作为其正常操作的一部分。