Checked exceptions vs unchecked exceptions

Discussion in 'Java' started by Ahmed Moustafa, Jul 13, 2004.

  1. 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
     
    Ahmed Moustafa, Jul 13, 2004
    #1
    1. Advertising

  2. Ahmed Moustafa

    Tony Morris Guest

    Tony Morris, Jul 13, 2004
    #2
    1. Advertising

  3. Ahmed Moustafa wrote:
    > 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
     
    Stefan Waldmann, Jul 13, 2004
    #3
  4. Ahmed Moustafa

    Chris Smith Guest

    Ahmed Moustafa wrote:
    > 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
     
    Chris Smith, Jul 13, 2004
    #4
  5. Ahmed Moustafa

    Chris Uppal Guest

    Chris Smith wrote:

    > 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
     
    Chris Uppal, Jul 14, 2004
    #5
  6. Ahmed Moustafa

    Chris Smith Guest

    Chris Uppal wrote:
    > 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
     
    Chris Smith, Jul 14, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wendy S
    Replies:
    0
    Views:
    1,834
    Wendy S
    Feb 18, 2004
  2. exquisitus
    Replies:
    4
    Views:
    8,365
    Patricia Shanahan
    May 6, 2005
  3. runescience
    Replies:
    0
    Views:
    4,288
    runescience
    Feb 23, 2006
  4. Lew
    Replies:
    15
    Views:
    779
  5. RVic
    Replies:
    19
    Views:
    1,413
Loading...

Share This Page