Checked exceptions vs unchecked exceptions

A

Ahmed Moustafa

What is the difference between checked exception and unchecked
exceptions (from the program point of view and the JVM point of view)?

I have read in some docs, it's recommended to use unchecked exception
more, why?

Thanks in advance!

Ahmed
 
S

Stefan Waldmann

Ahmed said:
What is the difference between checked exception and unchecked
exceptions (from the program point of view and the JVM point of view)?

I have read in some docs, it's recommended to use unchecked exception
more, why?

Thanks in advance!

Ahmed

Hi,

checked Exceptions must be dealt with in either a try/catch block or by
declaring a "throws" in a method. Unchecked exceptions normally are
Runtime exceptions like NullPointerException or ClassCastException.

A simple rule of thumb: If it's an exception you can possibly deal with
(continue to run the program using some alternative code), use checked
exceptions. For exceptions that should never happen (if they do, it's a
bug), use unchecked (Runtime) exceptions which will come up to the
surface and displayed to the user. Like this you assure that if there's
a bug, it will show up eventually and can be fixed, and you don't run
the risk of catching an exception and forgetting to deal with it (f.i.
empty catch block).

I once read an article about the subject which explains well when you
should use checked and unchecked exceptions, and why:

<http://www.mindview.net/Etc/Discussions/CheckedExceptions>

It's by Bruce Eckel, who also wrote (among other) "Thinking in Java".

Regards,
Stefan
 
C

Chris Smith

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
 
C

Chris Uppal

Chris said:
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).

I read an interview with Elliotte Rusty Harold where he quotes (I forget from
whom) a formulation that I find rather appealing.

Checked exceptions are for cases that aren't supposed to be found and
eliminated by testing.

(My paraphrase)

I'm not sure that I fully agree with it, but it does cut close to the nub of
why there are any checked exceptions at all.

-- chris
 
C

Chris Smith

Chris said:
I read an interview with Elliotte Rusty Harold where he quotes (I forget from
whom) a formulation that I find rather appealing.

Checked exceptions are for cases that aren't supposed to be found and
eliminated by testing.

This does pretty much sum it up. Pretty much the same thing as saying
that RuntimeException is used to indicate bugs in code, whereas checked
exceptions indicate other problems that the application needs to deal
with, but didn't cause. Error, of course, generally indicates a VM bug
(with the exception of a few, like AssertionError and OutOfMemoryError),
so is handled by a different stage of testing; but still testing.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top