Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why NullPointerException is a runtime exception and RemoteException not?

A possible reason because a NullPointerException is a runtime exception is because every method can throw it, so every method would need to have a "throws NullPointerException", and would be ugly. But this happens with RemoteException.

And a possible reason because RemoteException is not a runtime exception, is to tell it client to treat the exception. But every method in a remote environment need throws it, so there is no difference of throwing NullPointerException.

Speculations? Was I clear?

like image 648
The Student Avatar asked May 21 '10 19:05

The Student


People also ask

Why NullPointerException is runtime exception?

NullPointerException is a runtime exception in Java that occurs when a variable is accessed which is not pointing to any object and refers to nothing or null. Since the NullPointerException is a runtime exception, it doesn't need to be caught and handled explicitly in application code.

What is difference between runtime exceptions and plain exceptions?

Main difference between RuntimeException and checked Exception is that It is mandatory to provide try-catch or try finally block to handle checked Exception and failure to do so will result in a compile-time error, while in the case of RuntimeException this is not mandatory.

Is NullPointerException is an example of unchecked exception?

NullPointerException is an unchecked exception and extends RuntimeException class. Hence there is no compulsion for the programmer to catch it.


1 Answers

The way I understand it is:

  • RuntimeExceptions are thrown for things that were preventable.
  • Exceptions are thrown for things that were unpreventable but recoverable
  • Errors are thrown for things that were unpreventable and unrecoverable.

For example, NullPointerExceptions can always be avoided and are therefore unchecked exceptions. A RemoteException could occur when there is a network failure, which cannot be reasonably prevented before the method call and therefore is checked.

like image 199
Steve Avatar answered Sep 30 '22 07:09

Steve