开发者

Why is NullPointerException not declared as a checked exception

This was the question asked in an interview. NullPointerException is very common; why is it not declared as a checked exception? I googled but d开发者_JAVA技巧id not get a proper answer.


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.)


It's not a checked exception (among other things) because it is extremely common. It can occur pretty much everywhere. If it were checked, then nearly every single method in every single Java program anywhere would have to declare that it throws NullPointerException.


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.


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.

A NullPointerException (usually) occurs because there is some bug in your code. If you expect a NullPointerException to be thrown, the correct solution is to fix the bug rather than to handle the exception.


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.).

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?


My own obligatory definition for checked Exception. Checked exceptions are exceptions that an API would raise in-case of a known undesirable situation arises

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.


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.


Why include it when every function you write will have to declare it ? Just to make your life simple.


IF Null pointer exception occurs your proram will halt.Hence it is uchecked exception.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜