• Post Reply Bookmark Topic Watch Topic
  • New Topic

Basis for making exception as checked or unchecked  RSS feed

 
J Mohandos
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know checked exception need to be checked at compile time and runtime exception need not be checked at compile time.

My question is not related to the definition.

The question is on what basis have they selected that FileNotFound exception is a checked exception and NullPointerException is an unchecked exception? Is it the random wish of the creator or is there reason behind why something is selected as checked exception and something as unchecked?
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, a checked exception is one you can recover from or respond to.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/new-java/93184-basis-making-exception-checked-unchecked.html
 
J Mohandos
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. So I make an object null and call a method on that object. So obviously I will get NullPointerException at runtime. So is it not something I can recover from by making the object as not null? How come NullPointerException is not a checked exception?

Is it wrong to post on multiple sites? I never got any response from that site, hence I posted here.

 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say you catch a NullPointerException (NPE) in your code. Now what do you do? If your program really can continue with the null reference, then that's fine, but most often a NPE means a programming error. You can't easily recover from it.

But what about a FileNotFound error? This could mean that a user has entered a bad file name. So you catch the exception and say something like, "File not found, please enter an existing file name," or whatever. The point is, this is not a programming error, it's user error.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some forums feel that cross-posting is "wrong." Here, it's okay as long as you say something like, "I posted this to some-java-forum.com too."
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A NullPointerException is usually an indication that there is a bug in your code - in principle a NullPointerException should never happen, and if it does, you have to go and take a look at the code and fix the bug.

A FileNotFoundException is a condition that you can expect to happen. It means your program is trying to open a file that doesn't exist. For example, a user might have entered an invalid filename into the application. Your application can easily deal with the error by showing the user a message and ask him to enter a correct filename.

Do you see the fundamental difference between these two kinds of things?

Oracle's Java Tutorials also has an explanation: The Catch or Specify Requirement

That said, it is not always so easy to decide whether something should be a checked or an unchecked exception, and in the JDK there are some exceptions that you could argue are of the wrong type. For example UnsupportedEncodingException is a checked exception, but it might have been better if this would have been an unchecked exception.

There is some controversy about whether checked exceptions were a good idea at all. They are a feature unique to Java (other languages have exceptions too, but no checked exceptions). Some people think that we shouldn't have had checked exceptions at all in Java. Oracle's Java Tutorials even has a page about this: Unchecked Exceptions — The Controversy.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I would agree with Jesper about going through the tutorials.
Whether an Exception is checked or unchecked depends entirely on its position in the inheritance tree.
You can find the opposite opinion to the second link of Jesper's by Googling checked exceptions an experiment which failed
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!