Forums Register Login

Basis for making exception as checked or unchecked

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
In general, a checked exception is one you can recover from or respond to.
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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."
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1057 times.
Similar Threads
Compiler vs. Runtime Errors
Should I know the runtime exceptions for the exam?
Userdefined Exception
Checked and Unchecked exceptions
compiler question help
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:30:23.