• 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

General Understanding of Exceptions

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Peers & Mentors,
First off my questions come from my study of "A Programmer's Guide to Java Certification" by Khalid and Rolf, second half of chapter five, around pages 154 to 158.
I am having trouble understanding what checked versus unchecked exceptions mean. I am tempted to ask what I will need to know for the exam, but I would REALLY like to get a better understanding than the bare minimum of this concept and not get just the basics. Without further postponement, let us move on.
First, all exceptions come from java.lang.Throwable, specifically Error and Exception. Now, RuntimeExceptions are derived from the class Exception. Now, according to the book Error, RuntimeException and all related subclasses are unchecked exceptions. Any other exception is classified as a checked exception. Later, on page 155 they state that new exceptions are created by extending the Exception class or its subclasses thereby making them checked exceptions.
But wait�doesn�t RuntimeException come from Exception and it is unchecked? How can this be? So�I could extend RuntimeException and since the subclasses of RuntimeException are unchecked my new exception would be unchecked. Hmm, but we just said that extending Exception or its subclasses thereby creates checked exceptions.
As you can tell, I�m running circles and I hope someone has an EASY way for me to tell checked and uchecked exceptions apart. I just want to be able to identify them easily and have a better working knowledge of what to do with them.
Finally, it is my understanding that checked exceptions must be dealt with via a try-catch-finally structure or the method containing the checked exception creation must declare that it throws that exception(or a superclass of that exception). Is THIS correct?
Thanks everybody!
Sincerely,
Jason

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jason!
I'm not a guru on this, but:
Checked exceptions are exceptions that you should expect to happen from time to time that does not really relate to how your app executes, but rather to external factors like i/o errors, bad URLs and such.
Unchecked exceptions are exceptions you can deal with in the same way, by using try/catch, but is advised not to, as these exceptions are regarded as results of bad programming. For example an ArithmeticExeption thrown by a division by zero, is regarded as something you should take care of in your code, instead of catching the possible exception.
The reason for this is performance, as Exceptions are qiute costly, and shuold not be thrown when not necessary.
Regards,
Marius
 
Jason Stortz
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Marius,
Hmm. Okay, I can see that. The performance bit is a good point.
Thanks.
 
author
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jason, Exceptions that are not of type RuntimeException or Error are by default, checked exceptions. If you derive a class from Exception, it is checked and it is not a RuntimeException (unchecked). If you derive a class from RuntimeException, it is a subclass of Exception, but unchecked because it is a subclass of RuntimeException.
Checked exceptions must be either caught or listed on the throws clause of the method they are generated from. This is required by the language and enforced by the compiler. In other words, if you forget to catch or list a checked exception on the throws clause, your code will not compile.
I hope this helps,
Peter Haggar
------------------
Senior Software Engineer, IBM
author of: Practical Java
 
Jason Stortz
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter,
Thanks. Yes that does sort it out. I was just *this* close to understanding, but that's what I needed. I wasn't sure if there was some sort of default rules or what not and that helped.
Thank you for your time. I really appreciate it. Now that I understand it better I can try applying it to some code examples and enforcing my understanding of the concepts.
Jason
 
He puts the "turd" in "saturday". Speaking of which, have you smelled this 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