检索方法是否返回“null”,如果不能产生返回值,则抛出异常?

编程入门 行业动态 更新时间:2024-10-28 18:29:54
本文介绍了检索方法是否返回“null”,如果不能产生返回值,则抛出异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果没有找到,我应该如何:

  • 返回null
  • 抛出异常
  • 其他
  • 解决方案

    作为一般规则,如果该方法应该始终返回一个对象,那么请继续执行异常。如果您预期偶尔会出现null并且想以某种方式处理它,请与null一起使用。

    无论你做什么,我强烈建议第三个选项:返回一个字符串,说WTF。

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

    If it is not found, should I:

  • return null
  • throw an exception
  • other
  • 解决方案

    As a general rule, if the method should always return an object, then go with the exception. If you anticipate the occasional null and want to handle it in a certain way, go with the null.

    Whatever you do, I highly advise against the third option: Returning a string that says "WTF".

    更多推荐

    检索方法是否返回“null”,如果不能产生返回值,则抛出异常?

    本文发布于:2023-10-20 01:23:53,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1509415.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:抛出   返回值   异常   方法   null

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!