• 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

Exception Handling

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rancher's, Can anybody list "Checked & Unchecked Exceptions" as it creates lot of confusions to analyze ...
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe there are a lot of similar threads like this here. Have you tried to search it? And don't forget, Google is your best friend
 
sunil langeh
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Freddy for quick reply, ya i tried many of the same threads but still failure to found out the exact,,,,,
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Better Still. check the API
http://java.sun.com/j2se/1.5.0/docs/api/

All Subtypes of class Exception are checked exceptions except for RuntimeException and its subclasses.
RuntimeException are unchecked exception and so are its subclasses.

-VB
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In summary,
checked -> you need to catch or re-throw it. If you re-throw it, you need to declare the exception. It extends from Exception
unchecked -> you don't need to catch it. If you re-throw it, you don't need to declare the exception. It extends from RuntimeException

Hope this helps.
 
sunil langeh
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Freddy & Vinayak, I have one more doubt, Why is it so that checked exceptions must be handled or declared and not RuntimeException(and its subclasses) while both are inherited from Exception. If something like this:
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why is it so that checked exceptions must be handled or declared and not RuntimeException


Because it's defined in the Java Language Specification
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some background information:

The people who invented the Java language thought it was a good idea to have to kinds of exceptions: checked and unchecked exceptions. The idea was probably that checked exceptions should be used for normal, "expected" error conditions, and unchecked exceptions should be used for unexpected error conditions in a program (for example errors that would indicate that's there a bug in the program).

Other programming languages have exceptions too, but Java is unique in that it has two kinds of exceptions.

There has been some debate on the Internet on whether it was a good idea to have these two kinds of exceptions. There are some people who think that checked exceptions are not useful and should not have been put into the language.
 
Vinayak Bhat
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sunil langeh wrote:Thanks Freddy & Vinayak, I have one more doubt, Why is it so that checked exceptions must be handled or declared and not RuntimeException(and its subclasses) while both are inherited from Exception. If something like this:



Sunil, Checked Exception are compile time errors, if you don't handle them the compiler won't compile the code and you won't get the class file. So you are not getting any further. Runtime exception, the compiler doesn't bother to check. You can go through the KS && BB SCJP book. I am sure lots of your questions will be answered in the book.
Cheers
Vinayak
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vinayak Bhat wrote:Sunil, Checked Exception are compile time errors, if ...


It's a bit confusing to say "checked exceptions are compile time errors" - an exception is not a compile-time error.

Checked exceptions are called 'checked' because the compiler checks if you properly handle them in your code (by catching them or declaring with a 'throws' declaration that your method throws them to the calling method).

For unchecked exceptions, the compiler does not require you to handle the exception.
 
reply
    Bookmark Topic Watch Topic
  • New Topic