捕捉和抛出nullpointerexception的最佳做法? [英] Best practise in catching and throwing nullpointerexception?

查看:638
本文介绍了捕捉和抛出nullpointerexception的最佳做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎抓住nullpointerexception不是一个好主意。如果是这样的话,为什么它会被方法抛出?它是否应该被Exception捕获?

It seems like it is not a good idea to catch a nullpointerexception. If that's the case, why is it thrown by methods? Should it just be caught with Exception?

另外,如果我遇到一个null的参数(可以是像string这样的基本类型或带有字段的类),应该怎么做?我处理了吗? (假设没有扔npe)?

Also, if I encounter a parameter which is null (can either be primitive type like string or a class with fields), how should I handle it? (assumingly not throw npe)?

谢谢

推荐答案

nullpointerexception通常表示程序员错误。如果你不期望这样的程序员错误,你将不会捕获空指针异常。例如,假设您使用一个P类型的参数实现了一个myMethod方法。

A nullpointerexception usually indicates a programmer error. You won't catch a null pointer exception if you don't expect such a programmer error. For instance, say you implement a method myMethod with one parameter of type P.

void myMethod(P x)

假设您不希望参数x为空。在这种情况下,您可能不会检查null。为什么?因为您假设正在使用您的方法的程序员将正确使用它并且不会传递null。在这种情况下,我不是说你应该或不应该检查是否为null。我只是想回答为什么有时一个方法抛出空指针异常并且不应该被捕获。原因如下:您的方法可能抛出空指针异常,因为有人没有正确使用它(传递null)。使用您的方法的程序员可能知道如果该方法使用错误,它可能会抛出nullpointerexcetption。但即使他使用您的方法错误,也不会捕获异常,因为每个人都认为该方法没有被滥用。

Let's say you don't expect the parameter x to be null. In this case you might not check for null. Why? because you assume that the programmer who is using your method will use it properly and won't pass null. I'm not saying whether you should or shouldn't check for null in such cases. I'm just trying to answer why sometimes a null pointer exception is thrown by a method and isn't supposed to be caught. Here's why: Your method might throw a null pointer exception because someone didn't use it right (passed null). The programmer who is using your method might know that if the method is being used wrong it might throw a nullpointerexcetption. But even if he uses your method wrong the exception won't be caught because everyone assumes that the method isn't misused.

第二个问题:首先,String是一个Object,而不是一个原始类型。其次,原语不能为空(原语是整数,双精度,浮点数,字符,布尔值和其他一些)。现在,如果您遇到一个null参数并且它不应该为null(换句话说,您收到了非法参数),您有一些选择:

Your second question: First, a String is an Object, not a primitive type. Second, primitives can't be null (primitives are ints, doubles, floats, chars, booleans and some others). Now, if you encounter a parameter which is null and it shouldn't be null (in other words you received an illegal argument) you have some choices:


  • 抛出 IllegalArgumentException 指示参数为null,并且您的方法期望它不为null。

  • 不执行任何操作。假设您的方法正确使用(换句话说,假设没有程序员错误)。

  • 使用断言断言(x!= null);

  • Throw an IllegalArgumentException indicating the parameter is null and your method expects it not to be null.
  • Don't do anything. Assume your method is being used correctly (in other words, assume no programmer errors).
  • Use assertions: assert(x != null);

我个人不是防御的粉丝编程,通常更喜欢第二种选择。但是,如果我想确保变量的一些不变量,我正在使用断言。但这些只是我的习惯而其他人可能不同意。

I personally am not a fan of defensive programming and usually prefer the second option. But still, if I want to ensure some invariants on my variables I'm using assertions. But these are just my habits and others probably disagree.

这篇关于捕捉和抛出nullpointerexception的最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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