捕获和抛出空指针异常的最佳实践? [英] Best practise in catching and throwing nullpointerexception?

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

问题描述

捕获空指针异常似乎不是一个好主意.如果是这样,为什么它会被方法抛出?它应该被异常捕获吗?

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?

另外,如果我遇到一个为空的参数(可以是像字符串这样的原始类型,也可以是带有字段的类),我应该如何处理它?(假设不会扔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)?

谢谢

推荐答案

空指针异常通常表示程序员错误.如果您不期望这样的程序员错误,您将不会捕获空指针异常.例如,假设您使用一个 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.我只是想回答为什么有时方法会抛出空指针异常并且不应该被捕获.原因如下:您的方法可能会抛出空指针异常,因为有人没有正确使用它(传递空值).使用您的方法的程序员可能知道,如果该方法使用错误,则可能会抛出空指针异常.但即使他错误地使用了您的方法,也不会捕获异常,因为每个人都认为该方法没有被误用.

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 是一个对象,而不是原始类型.其次,原语不能为空(原语是整数、双精度、浮点数、字符、布尔值和其他一些).现在,如果您遇到一个为 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 表示参数为空,您的方法期望它不为空.
  • 什么都不做.假设您的方法使用正确(换句话说,假设没有程序员错误).
  • 使用断言:assert(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.

这篇关于捕获和抛出空指针异常的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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