• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is null pointer exception the only type of exception we are not supposed to catch?

 
Monica Shiralkar
Ranch Hand
Posts: 826
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are not supposed to catch null pointer exception. What is the reason for that?Is null pointer exception the only type of exception we are not supposed to catch?
thanks
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The page The Catch or Specify Requirement in Oracle's tutorial about exceptions explains about the three kinds of exceptions and what you are or are not supposed to catch.

In general, you're required to handle checked exceptions, and you're not supposed the catch unchecked exceptions and errors.

NullPointerException is an example of an unchecked exception. All exceptions that are subclasses of RuntimeException are unchecked exceptions.
 
Winston Gutkowski
Bartender
Pie
Posts: 10091
56
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monica. Shiralkar wrote:We are not supposed to catch null pointer exception. What is the reason for that?

In addition to the fact that NPEs are unchecked, there's also the fact that they are thrown automatically for you by the JVM.

You won't see code with:
throw new NullPointerException();
in it very often, because it is usually an effect, not a cause - ie, it usually indicates that there is something else wrong in your program.

Exceptions like this are relatively rare, but they also include ArithmeticException and ArrayIndexOutOfBoundsException (and probably some others).

I should add that NPEs can be some of the most pernicious and nasty errors to try and track down, specifically because they are the effect, not the cause; so try and avoid them if you possibly can.

HIH

Winston
 
Monica Shiralkar
Ranch Hand
Posts: 826
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all.

In general, you're required to handle checked exceptions, and you're not supposed the catch unchecked exceptions and errors.


So does that mean that exception handling with Try-Catch is only for Checked Exceptions. We should never put try catch around runtime exceptions although java allows that(in that case I suppose it is only for internal working of java not for us)
 
Chan Ag
Rancher
Posts: 1089
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So does that mean that exception handling with Try-Catch is only for Checked Exceptions. We should never put try catch around runtime exceptions although java allows that(in that case I suppose it is only for internal working of java not for us)


Sometimes we also want to handle some program logic error, do validation of fields - while constructing objects or otherwise. In such cases, we do handle/throw RuntimeExceptions, sometimes to prevent a failure altogether or to provide a more descriptive message after the failure.

You might want to check this very nice article here in ranch - https://www.coderanch.com/how-to/java/FirstClasses
The part relevant to your question is partially covered in the validation section.

I also feel that sometimes throwing an IllegalArgumentException might be a better thing than to have the JVM throw the notorious NPE. I leave that to further finding or to the others for comments.

Of course there is more to it. But this is what I have found so far.

Chan.
 
Winston Gutkowski
Bartender
Pie
Posts: 10091
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monica. Shiralkar wrote:So does that mean that exception handling with Try-Catch is only for Checked Exceptions. We should never put try catch around runtime exceptions although java allows that(in that case I suppose it is only for internal working of java not for us)

No, not at all. There's absolutely nothing to stop you using try...catch with unchecked exceptions, especially if you can reasonably expect to recover gracefully from the error and continue execution (as may be the case with IllegalArgumentException).

All the writer of the method that throws the exception is saying is: "you don't have to use try...catch (or throws) with this method".

BTW, I believe with the link that Chan gave you, he is referring specifically to this section (it's quite a large "page"). I should also add that its still something of a work-in-progress, which is why I haven't linked to it yet; but most of the narrative is complete, and if you find it useful, that's great.

Winston
 
Monica Shiralkar
Ranch Hand
Posts: 826
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you
 
Parvathi Raju
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All exception class that extends RuntimeExceptions are unchecked exception.We can catch NullPointerException. But not able to find out at compile time.we can predict only at runtime
 
Vivek Kr Agrawal
Ranch Hand
Posts: 51
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
The unchecked exceptions especially null pointer exception are largely result of poor programming and methodology. So by doing coding in a better way it can be avoided. Having said that, checked exceptions like FileNotFound and IOException are forced by the compiler. They are meant to be catched and the user can recover from these failures.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic