why checked and unchecked exception?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #1

    why checked and unchecked exception?

    why java provides two flavour exceptions?

    why java tells programmer to handle or throw anyway some exceptions and some not?
    as jvm can catch it and throw it then why java provided so?

    plz explain ....
    Regards.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Some exceptions can't be checked by the compiler; think of NullPointerExce ption
    or OperationNotSup portedException . Only during runtime these exceptional
    conditions can show up. Maybe the best example is: OutOfMemoryExce ption,
    i.e. there is no way a compiler can check that condition and neither can you or
    anyone else. That's why it was made an unchecked exception.

    The class of the checked exceptions indicate events that in real life can happen
    and your program should deal with them: an IOException is a nice example.
    That's the reason why it was made a checked exception.

    kind regards,

    Jos

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      can we catch or should we catch the OutOfMemoryExce ption?

      Is this the subclass of Exception?

      Plz help.
      Regards.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by dmjpro
        can we catch or should we catch the OutOfMemoryExce ption?

        Is this the subclass of Exception?

        Plz help.
        Regards.
        All bets are off when the JVM is out of memory; of course you can catch that
        error but all you can do is release (a substantial amount of) memory by setting
        a bunch of references to null. Most of the time when this error is thrown there's
        an error in your program logic.

        kind regards,

        Jos

        Comment

        Working...