wood burning stoves 2.0*
The moose likes Java in General and the fly likes Is null pointer exception the only type of exception we are not supposed to catch? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is null pointer exception the only type of exception we are not supposed to catch?" Watch "Is null pointer exception the only type of exception we are not supposed to catch?" New topic
Author

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

Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
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

Joined: Aug 16, 2005
Posts: 14157
    
  19

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7831
    
  21

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
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
Bartender

Joined: Sep 06, 2012
Posts: 1018
    
  15
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

Joined: Mar 17, 2011
Posts: 7831
    
  21

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

Joined: Jul 07, 2012
Posts: 644
thank you
Parvathi Raju
Greenhorn

Joined: Aug 04, 2013
Posts: 9
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

Joined: Sep 08, 2012
Posts: 51

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.


Vivek Agrawal
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is null pointer exception the only type of exception we are not supposed to catch?