抛出异常或返回null [英] Throw an exception or return null

查看:556
本文介绍了抛出异常或返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有下面的函数,有两个选择

If I've got the function below, with two choices

private MyObject findBlank() {
    for (int i = 0; i < pieces.length; i++) {
        if(pieces[i].isBlank()){
            return pieces[i];
        }
    }
    return null;
}

private MyObject findBlank() {
    for (int i = 0; i < pieces.length; i++) {
        if(pieces[i].isBlank()){
            return pieces[i];
        }
    }
    throw new NoSuchFieldError("No blank piece found!");
}



从这个方法,我知道应该总是返回一个对象其中一个'件'总是 isBlank()== true ,结尾的返回null只是为了请编译器。既然是这样,我的代码不会工作,如果它返回null,这是正确的,请抛出异常?

From this method I know that it should always return an object one of the 'pieces' always is isBlank() == true , the return null at the end is just to please the compiler. Since this is the case and my code wouldn't work anyway if it returned null, is this the correct please to throw an exception?

我的选项是:


  1. 返回null,应用程序将在某些边缘情况下获得NullPointerException

  2. 返回null,具有(myObject!= null)的方法检查

  3. 抛出异常,将在运行时将其抛出。


$ b b

我猜我要问的是,这是正确的地方抛出异常吗?即如果它进入情况,我没有什么能做的。这是归类为异常还是应该检查我的方法返回什么(这使我的代码看起来可怕)。如果我知道它不应该返回null,那么我应该抛出异常吗?

I guess what I'm asking is, is this the correct place to throw an exception? i.e. there is nothing I can do about it if it gets into the situation. Is this classed as 'exceptional' or should I null check what my method returns (which makes my code look horrible). If I know it shouldn't return null then I should throw the exception right?

此外,我如何选择什么异常,或扩展一个并抛出自己的异常?

Also how would I choose what exception, or extend one and throw my own?

推荐答案

关于您的选项,问自己是否

About your options, ask yourself if


  1. 这是一个好主意,此方法返回一个意外的值(即 null )?

  2. 如果你屏蔽了 null return value?

  3. 这是一个好主意,立即爆炸,只是因为有一个错误的值?

  1. Is it a good idea to have your program blow up at some point after this method returned an unexpected value (i.e. null)?
  2. What exactly will be hidden if you mask out the null return value?
  3. Is it a good idea to blow up immediately, just because there was a wrong value?

个人我会选择2或3,取决于我是否更喜欢问题2或3的答案。选项1绝对是一个坏主意,特别是如果它不应该发生。如果程序在你的函数返回后抛出一个NPE方法,你将很难找出 null 的来源。

Personally I'd go for option 2 or 3, depending on whether I like the answer to question 2 or 3 better. Option 1 definitely is a bad idea, especially if it's not supposed to happen. If the program throws a NPE way after your function returned, you'll have a hard time figuring out where the null came from. Especially if it happens months after you finished working on this particular function.

如果您选择抛出异常,您会立即看到在哪里错误,您可以直接到那里找出为什么出错了。返回 null 并在调用函数中检查它也可以工作,但是只有当你不会默默失败,但实际上做一些事情正确处理问题。

If you choose to throw an exception, you immediately see where something went wrong and you can directly go there to figure out why it went wrong. Returning null and checking for it in the calling function could also work, but only if you don't fail silently, but actually do something to handle the problem properly.

这篇关于抛出异常或返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆