Ahmed said:
What is the difference between checked exception and unchecked
exceptions (from the program point of view and the JVM point of view)?
Stefan covers this briefly. If you need more detail on the language
characteristics, go ahead and ask more specific questions.
I have read in some docs, it's recommended to use unchecked exception
more, why?
There are a number of reasons that some people push toward unchecked
exceptions. Some of them have merit, but most of them do not; so watch
out with the advice you've gotten here. Unchecked exceptions should be
used for things that you don't, in general, intend to respond to (or
don't expect the person using your code to respond to). They are
thrown, for example, in response to coding bugs (as is the case with
RuntimeException and the like). Checked exceptions should be used for
anything you (or the person using your code) do intend to hand and
respond to.
Unfortunately, you're not always in the best position to make such a
choice when you're writing code; some clients may want to respond to a
certain condition, while others may want to just fail. The guideline
should be redefined, then, to say that a checked exception should be
used whenever ANY normal client MIGHT want to respond. It's really
quite easy, in the end, to ignore a checked exception if you don't care
to handle it, but it's dangerous to rely on your clients to remember to
add handling for an exceptional condition that you didn't enforce.
The arguments for unchecked exceptions deal mostly with convenience in
writing code. If you do exception handling right (including abstraction
and the like), there will be a fair amount of code involved in catching
exceptions, converting their types to something more appropriate to the
local abstraction, etc. People feel like they can get away with not
doing that stuff if the exception isn't checked. In reality, they still
ought to do it, but it's just easier to ignore that responsibility and
end up with bad code.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation