views:

301

answers:

7

Hi This was the question asked in interview, null pointer exception is very common why it is not declared as checked exception.I googled but did not get proper answer.

Thanks

+19  A: 

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

Bozho
+1 - It will make the code very very ugly.
duduamar
Also: catching a `NullPointerException` specifically is almost always a bad idea.
Joachim Sauer
+6  A: 

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.

Chris Knight
+9  A: 

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.

Michael Borgwardt
+2  A: 

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?

Yishai
+2  A: 

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.

ring bearer
The should be caught high up in the call stack to deliver a better user experience, in my opinion
Yaneeve
This sounds like Dick Cheney's "known unknowns" and "unknown unknowns" ;-).
Stephen C
@yaneeve, I think by "caught" you meant a check like `if(myList != null)` kind of statement. Other than that, there should not be anything like `catch(NullPointerException npe)` in a code. That should *never* pass a code-review.
ring bearer
I don't see why an IOException is more of a "known undesirable" than a NullPointerException. Don't you know there is a >99% chance that your program has at lease one bug in it?
Yishai
An NPE is the result of a programmer error. Even a perfect coder can't control whether they get an IOException. That's the difference.
DJClayworth
@Yishai - IOException is a "known undesirable"- because for ex: when I call `createNewFile()` a known undesirable such as disk out of space might occur.
ring bearer
@ring bearer, I agree that it is a "known undesirable," it just seems that NPE is in the same category.
Yishai
@DJClayworth, I agree with your distinction as the basic distinction of what should and should not be a checked exception, but "known undesirable" doesn't seem to capture it.
Yishai
+1  A: 

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.

Heinzi
@Heinzi - I would not agree with "occur because something in the environment" point. For example, a JAAS login moudle could throw `LoginException` when credentials are incorrect. Or an `AccountService` could throw `InsufficientBalanceException` in some business scenario. So they are not "Environment" related per se.
ring bearer
Well, one could consider user input and data store as part of the "Environment", in that they are external to the logic of the code itself.
Dave Costa
@ring bearer, a LoginException is a configuration (environment) issue in most cases (the user is not configured, or not configured with that password). Checked exceptions can be used the way you describe for AccountService, but since that is part of a theoretical home-grown API, I don't think it is a good example.
Yishai
@Yishai Let me summarize in two sentences.Checked – client(caller) must take recovery action(Environment or otherwise)Unchecked – programming error
ring bearer
A: 

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