• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exceptions

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why exception in java has been classified into checked and unchecked
exception rather than keeping it under simply exception?
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why exception in java has been classified into checked and unchecked
exception rather than keeping it under simply exception?



There are actually 3 types. Checked Exceptions, Runtime Exceptions and Errors. Errors are similar to Exceptions but they are reserved for VM errors like out of memory conditions.

When you have a method, it's name usually indicates what it does, one of the reason for having checked exceptions is to indicate what the method can't do.

Example:



So for someone using this method what is it saying?

readFile: Hi, I'm readFile. If you pass a file name string to me I can read it and return the contents as a string. But if the file's not there, I'm sorry but I can't handle that. I'm just giving up and throwing it back at you. Really sorry, but I just can't deal with this situation.

The reason is that this method might be common and could be used with a web application, web service, desktop app, cell phone, etc. Each might would want to hand this situation differently.


This is a simple example. There are many cases where you might not want to throw an exception. This is one of the great debates. If you google you'll find many articles and threads on this topic.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

the difference between checked an unchecked exception is, for an unchecked exception it is not required to handle or declare it.


Imagine what would happen if all exceptions were checked, NullPointerException for example.
Or if all were unchecked, IOException for example.

Yours,
Bu.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi....

unchecked exceptions are the ones where the fault lies with programmer, where as checked exceptions are the ones where programmer may not be at fault....
unchecked or Run time exception can occur due to faulty design of your code such as dividing integer by zero.... Immagine what would have happened if it was put under checked exception, you would then have to provide try catch block to every integer you use thus making code difficult to read and write....
As for checked exception, they are the ones on which programmer don't have any control.... File name passed to program may not be present or corrupted....
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Checked exceptions were a fairly new experiment in Java. They are consistent with compile time type checking in the rest of the language but few (any?) other languages use them. There is much debate on whether they were a good idea overall or not. See the Exceptions chapter in Thinking In Java for a good explanation of the pros & cons.
 
Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic