java 为什么 NullPointerException 未声明为已检查异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2683958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is NullPointerException not declared as a checked exception
提问by giri
This was the question asked in an interview. NullPointerExceptionis very common; why is it not declared as a checked exception? I googled but did not get a proper answer.
这是在一次采访中被问到的问题。NullPointerException很常见;为什么不将其声明为已检查异常?我用谷歌搜索但没有得到正确的答案。
回答by Bozho
Almost every method would have to declare throwing it.
几乎每个方法都必须声明抛出它。
public void myMethod(String param) throws NullPointerException {
//
}
(As a sidenote - Eclipse for example gives you a warning whenever there is a "potential null pointer access" so that you can prevent the exception as early as possible.)
(作为旁注 - 例如,只要有“潜在的空指针访问”,Eclipse 就会向您发出警告,以便您可以尽早防止异常。)
回答by Michael Borgwardt
It's not a checked exception (among other things) becauseit is extremely common. It can occur pretty much everywhere. If it were checked, then nearly every single method in every single Java program anywherewould have to declare that it throws NullPointerException.
这不是受检异常(除其他外),因为它非常常见。它几乎可以在任何地方发生。如果它被检查,那么几乎每个 Java 程序中的每个方法都必须声明它throws NullPointerException。
回答by Chris Knight
Null pointer exceptions are extensions of Runtime exceptions and are therefore unexpected occurances in your program flow. It wouldn't make sense to expect a null pointer exception to be thrown (I hope!) and therefore you would never declare it as a checked exception.
空指针异常是运行时异常的扩展,因此是程序流中的意外事件。期望抛出空指针异常是没有意义的(我希望!),因此您永远不会将其声明为已检查异常。
回答by Yishai
The one sentence answer I would give is that it is the result of a programming error, and programming error exceptions are not checked exceptions (IllegalStateException, ClassCastException, etc.).
我给的一句话回答是,它是编程错误的结果,编程错误异常不是检查异常(IllegalStateException,ClassCastException等)。
But even if you had an argument for why it should maybe be a checked exception, basically every operation on an object reference can throw it, so it would be all over the place and literally every method in a non-trivial program would have to throw it - so what would be the point?
但是,即使你有一个关于为什么它应该是一个检查异常的论点,基本上对对象引用的每个操作都可以抛出它,所以它会到处都是,而且非平凡程序中的每个方法都必须抛出它 - 那有什么意义呢?
回答by Heinzi
Checked exceptions can occur because something in the environment, over which your program has little or no control, went wrong (e.g. IOException, SQLException). You can anticipate that and handle that case.
检查异常的发生可能是因为您的程序几乎无法控制或无法控制的环境中的某些事情出错了(例如IOException,SQLException)。您可以预见并处理这种情况。
A NullPointerException(usually) occurs because there is some bug in your code. Ifyou expect a NullPointerException to be thrown, the correct solution is to fix the bug rather than to handle the exception.
A NullPointerException(通常)发生是因为您的代码中存在一些错误。如果您希望抛出 NullPointerException,正确的解决方案是修复错误而不是处理异常。
回答by ring bearer
My own obligatory definition for checked Exception. Checked exceptions are exceptions that an API would raise in-case of a known undesirable situation arises
我自己对检查异常的强制性定义。已检查异常是 API 在出现已知不良情况时会引发的异常
NullPointerExceptions does not indicate a "known undesirable" situation. Instead, those are generally thrown due to some unhanded situations in the code. That is, they are most of the time due to bad coding practices - Such as trying get size of a list which is not initialized properly etc. So, there is no point in making them checked exceptions - as every object in Java could be null at some point?!. NullPoitnerException`s never should be caught either.
NullPointerExceptions 并不表示“已知的不良”情况。相反,这些通常是由于代码中的一些未经处理的情况而被抛出的。也就是说,它们大部分时间是由于糟糕的编码实践 - 例如尝试获取未正确初始化的列表的大小等。因此,让它们检查异常是没有意义的 - 因为 Java 中的每个对象都可能为 null在某一点?!。NullPoitnerException 也不应该被捕获。
回答by java_mouse
Checked exceptions are only for the exceptions where the program can recover. Invoking something on a NULL object is a programmers issue and can not be recovered.
已检查异常仅针对程序可以恢复的异常。在 NULL 对象上调用某些东西是程序员的问题并且无法恢复。
回答by LONGHORN007
IF Null pointer exception occurs your proram will halt.Hence it is uchecked exception.
如果发生空指针异常,您的程序将停止。因此它是 uchecked 异常。
回答by user324138
Why include it when every function you write will have to declare it ? Just to make your life simple.
当您编写的每个函数都必须声明它时,为什么要包含它?只为让你的生活变得简单。

