Should a retrieval method return 'null' or

2018-12-31 18:26发布

I have a method that is supposed to return an object if it is found.

If it is not found, should I:

  1. return null
  2. throw an exception
  3. other

30条回答
冷夜・残月
2楼-- · 2018-12-31 18:58

Depends on what it means that the object is not found.

If it's a normal state of affairs, then return null. This is just something that might happen once in an while, and the callers should check for it.

If it's an error, then throw an exception, the callers should decide what to do with the error condition of missing object.

Ultimately either would work, although most people generally consider it good practice to only use Exceptions when something, well, Exceptional has happened.

查看更多
时光乱了年华
3楼-- · 2018-12-31 18:58

Generally it should return null. The code calling the method should decide whether to throw an exception or to attempt something else.

查看更多
弹指情弦暗扣
4楼-- · 2018-12-31 18:59

Only throw an exception if it is truly an error. If it is expected behavior for the object to not exist, return the null.

Otherwise it is a matter of preference.

查看更多
呛了眼睛熬了心
5楼-- · 2018-12-31 18:59

I prefer to just return a null, and rely on the caller to handle it appropriately. The (for lack of a better word) exception is if I am absolutely 'certain' this method will return an object. In that case a failure is an exceptional should and should throw.

查看更多
只靠听说
6楼-- · 2018-12-31 19:01

If you are always expecting to find a value then throw the exception if it is missing. The exception would mean that there was a problem.

If the value can be missing or present and both are valid for the application logic then return a null.

More important: What do you do other places in the code? Consistency is important.

查看更多
余生请多指教
7楼-- · 2018-12-31 19:01

If the method returns a collection, then return an empty collection (like sayed above). But please not Collections.EMPTY_LIST or such! (in case of Java)

If the method retrives a single object, then You have some options.

  1. If the method should always find the result and it's a real exception case not to find the object, then you should throw an exception (in Java: please an unchecked Exception)
  2. (Java only) If you can tolerate that the method throws a checked exception, throw a project specific ObjectNotFoundException or the like. In this case the compiler says you if you forget to handle the exception. (This is my preferred handling of not found things in Java.)
  3. If you say it's really ok, if the object is not found and your Method name is like findBookForAuthorOrReturnNull(..), then you can return null. In this case it is strongly recomminded to use some sort of static check or compiler check, wich prevents dereferencing of the result without a null check. In case of Java it can be eg. FindBugs (see DefaultAnnotation at http://findbugs.sourceforge.net/manual/annotations.html) or IntelliJ-Checking.

Be careful, if you decide to return a null. If you are not the only programmer in project you will get NullPointerExceptions (in Java or whatever in other Languages) at run time! So don't return nulls which are not checked at compile time.

查看更多
登录 后发表回答